Add actionVersion test

This commit is contained in:
Luis Blanco 2023-01-03 22:55:49 +04:00
parent fd165d935d
commit 3d02cfb49c
4 changed files with 148 additions and 102 deletions

View File

@ -1,16 +1,10 @@
name: Publish name: Publish to NPM
defaults:
run:
shell: bash
on: on:
workflow_dispatch: workflow_dispatch
inputs:
name:
description: 'Release name'
required: true
default: 'Minor Update'
text:
description: 'Patch notes'
required: true
default: Fixed minor issues.
jobs: jobs:
Publish: Publish:
@ -20,16 +14,19 @@ jobs:
steps: steps:
- name: Fetch Repository - name: Fetch Repository
uses: actions/checkout@v2 uses: actions/checkout@v3
with:
persist-credentials: false
- name: Install Node.js - name: Install Node.js
uses: actions/setup-node@v1 uses: actions/setup-node@v3
with: with:
node-version: 18.12.1 node-version: 18.12.1
cache: 'npm'
- name: Get Npm Version - name: Get Package Version
id: package-version id: package-version
uses: martinbeentjes/npm-get-version-action@master run: node -e \"require('./utils').actionVersion()\" >> $GITHUB_OUTPUT
- name: Publish - name: Publish
run: | run: |
@ -38,11 +35,12 @@ jobs:
env: env:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }} NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
- name: GitHub Release - name: Create Release
uses: actions/create-release@v1 id: create_release
uses: softprops/action-gh-release@v1
env: env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with: with:
tag_name: ${{ steps.package-version.outputs.current-version }} tag_name: ${{ steps.package-version.outputs.version }}
release_name: ${{ github.event.inputs.name }} name: Release ${{ steps.package-version.outputs.version }}
body: ${{ github.event.inputs.text }} body: Published at ${{ github.sha }}

View File

@ -1,4 +1,7 @@
name: Validate name: Validate
defaults:
run:
shell: bash
on: on:
push: push:

194
utils.d.ts vendored
View File

@ -2,81 +2,115 @@ import type { Writable } from 'stream';
declare module "addon-tools-raub/utils" { declare module "addon-tools-raub/utils" {
/** /**
* Install binaries * Install binaries
* Downloads and unzips the platform specific binary for the calling package. * Downloads and unzips the platform specific binary for the calling package.
* To use it, create a new script for your package, which may as well be named * To use it, create a new script for your package, which may as well be named
* **install.js**, with the following content: * **install.js**, with the following content:
* ``` * ```
* 'use strict'; * 'use strict';
* const install = require('addon-tools-raub/install'); * const install = require('addon-tools-raub/install');
* const prefix = 'https://github.com/USER/ADDON-NAME/releases/download'; * const prefix = 'https://github.com/USER/ADDON-NAME/releases/download';
* const tag = 'v1.0.0'; * const tag = 'v1.0.0';
* install(`${prefix}/${tag}`); * install(`${prefix}/${tag}`);
* ``` * ```
* * `prefix` - the constant base part of the download url. * * `prefix` - the constant base part of the download url.
* * `tag` - the version-dependent part of the url. * * `tag` - the version-dependent part of the url.
* ``` * ```
* "scripts": { * "scripts": {
* "postinstall": "node install" * "postinstall": "node install"
* }, * },
* ``` * ```
*/ */
export const install: (folder: string) => void; export const install: (folder: string) => void;
/** /**
* Copy binary * Copy binary
* Copies the addon binary from `src/build/Release` to the platform folder. * Copies the addon binary from `src/build/Release` to the platform folder.
* ``` * ```
* declare const cpbin: (name: string) => Promise<void>; * declare const cpbin: (name: string) => Promise<void>;
* export default cpbin; * export default cpbin;
* ``` * ```
* It is useful for development builds. Use it in your **src/package.json**: * It is useful for development builds. Use it in your **src/package.json**:
* ``` * ```
* "scripts": { * "scripts": {
* * "build": "node-gyp rebuild && node -e \"require('addon-tools-raub/cpbin')('ADDON')\"" * * "build": "node-gyp rebuild && node -e \"require('addon-tools-raub/cpbin')('ADDON')\""
* }, * },
* ``` * ```
* Here ADDON should be replaced with the name of your addon, without `.node` extension. * Here ADDON should be replaced with the name of your addon, without `.node` extension.
*/ */
export const cpbin: (name: string) => Promise<void>; export const cpbin: (name: string) => Promise<void>;
/** /**
* Download to memory * Package version for GitHub Actions
* Accepts an **URL**, and returns an in-memory file Buffer, * Example of `actionVersion` usage in **Github Actions**:
* when the file is loaded. Use for small files, as the whole *
* file will be loaded into memory at once. * ```
* * - name: Get Package Version
* ``` * id: package-version
* download(srcUrl).then(data => useData(data), err => emit('error', err)); * run: node -e \"require('addon-tools-raub/utils').actionVersion()\" >> $GITHUB_OUTPUT
* ``` * - name: Create Release
* or * uses: softprops/action-gh-release@v1
* ``` * with:
* const data = await download(srcUrl); * tag_name: ${{ steps.package-version.outputs.version }}
* useData(data); * ```
* ``` */
export const actionVersion: () => void;
/**
* Package version for GitHub Actions
* Example of `actionZip` usage in **Github Actions**:
*
* ```
* - name: Zip Files
* id: zip-files
* run: node action-zip >> $GITHUB_OUTPUT
* - name: Store Binaries
* uses: softprops/action-gh-release@v1
* with:
* files: ${{ steps.zip-files.outputs.zip }}
* ```
*/
export const actionZip: () => void;
/**
* Download to memory
* Accepts an **URL**, and returns an in-memory file Buffer,
* when the file is loaded. Use for small files, as the whole
* file will be loaded into memory at once.
*
* ```
* download(srcUrl).then(data => useData(data), err => emit('error', err));
* ```
* or
* ```
* const data = await download(srcUrl);
* useData(data);
* ```
*/ */
export const download: (url: string) => Promise<Buffer>; export const download: (url: string) => Promise<Buffer>;
/** /**
* (async) Read a file * (async) Read a file
* Reads a whole file to string, NOT A Buffer * Reads a whole file to string, NOT A Buffer
*/ */
/** /**
* WritableBuffer * WritableBuffer
* A [Writable](https://nodejs.org/api/stream.html#stream_writable_streams) * A [Writable](https://nodejs.org/api/stream.html#stream_writable_streams)
* stream buffer, that is stored in-memory and can be fully * stream buffer, that is stored in-memory and can be fully
* obtained when writing was finished. It is equivalent to stream-writing * obtained when writing was finished. It is equivalent to stream-writing
* a temporary file and then reading it into a `Buffer`. * a temporary file and then reading it into a `Buffer`.
*/ */
export class WritableBuffer extends Writable { export class WritableBuffer extends Writable {
constructor(); constructor();
/** /**
* Get the downloaded data * Get the downloaded data
* Use `stream.get()` to obtain the data when writing was finished * Use `stream.get()` to obtain the data when writing was finished
*/ */
get(): Buffer; get(): Buffer;
} }
@ -85,97 +119,97 @@ declare module "addon-tools-raub/utils" {
export const read: (name: string) => Promise<string>; export const read: (name: string) => Promise<string>;
/** /**
* (async) Write a file * (async) Write a file
*/ */
export const write: (name: string, text: string) => Promise<void>; export const write: (name: string, text: string) => Promise<void>;
/** /**
* (async) Copy a file * (async) Copy a file
*/ */
export const copy: (src: string, dest: string) => Promise<void>; export const copy: (src: string, dest: string) => Promise<void>;
/** /**
* (async) Check if a file/folder exists * (async) Check if a file/folder exists
*/ */
export const exists: (name: string) => Promise<boolean>; export const exists: (name: string) => Promise<boolean>;
/** /**
* (async) Create an empty folder * (async) Create an empty folder
*/ */
export const mkdir: (name: string) => Promise<void>; export const mkdir: (name: string) => Promise<void>;
/** /**
* (async) Get status on a file * (async) Get status on a file
*/ */
export const stat: (name: string) => Promise<Stats>; export const stat: (name: string) => Promise<Stats>;
/** /**
* (async) Check if the path is a folder * (async) Check if the path is a folder
*/ */
export const isDir: (name: string) => Promise<boolean>; export const isDir: (name: string) => Promise<boolean>;
/** /**
* (async) Check if the path is a file * (async) Check if the path is a file
*/ */
export const isFile: (name: string) => Promise<boolean>; export const isFile: (name: string) => Promise<boolean>;
/** /**
* Cut the path one folder up * Cut the path one folder up
*/ */
export const dirUp: (dir: string) => string; export const dirUp: (dir: string) => string;
/** /**
* (async) Create a directory * (async) Create a directory
* Like `mkdir -p`, makes sure a directory exists * Like `mkdir -p`, makes sure a directory exists
*/ */
export const ensuredir: (dir: string) => Promise<void>; export const ensuredir: (dir: string) => Promise<void>;
/** /**
* (async) Copy a file * (async) Copy a file
* Copy a file, `dest` folder is created if needed * Copy a file, `dest` folder is created if needed
*/ */
export const copysafe: (src: string, dest: string) => Promise<void>; export const copysafe: (src: string, dest: string) => Promise<void>;
/** /**
* (async) Read a directory * (async) Read a directory
* Get file/folder names of the 1st level * Get file/folder names of the 1st level
*/ */
export const readdir: (src: string, dest: string) => Promise<ReadonlyArray<string>>; export const readdir: (src: string, dest: string) => Promise<ReadonlyArray<string>>;
/** /**
* (async) List subdirectories * (async) List subdirectories
* Get folder paths (concatenated with input) of the 1st level * Get folder paths (concatenated with input) of the 1st level
*/ */
export const subdirs: (name: string) => Promise<ReadonlyArray<string>>; export const subdirs: (name: string) => Promise<ReadonlyArray<string>>;
/** /**
* (async) List nested files * (async) List nested files
* Get file paths (concatenated with input) of the 1st level * Get file paths (concatenated with input) of the 1st level
*/ */
export const subfiles: (name: string) => Promise<ReadonlyArray<string>>; export const subfiles: (name: string) => Promise<ReadonlyArray<string>>;
/** /**
* (async) Get all nested files recursively * (async) Get all nested files recursively
* Folder paths are omitted by default. * Folder paths are omitted by default.
* Order is: shallow-to-deep, each subdirectory lists dirs-then-files. * Order is: shallow-to-deep, each subdirectory lists dirs-then-files.
*/ */
export const traverse: (name: string, showDirs?: boolean) => Promise<ReadonlyArray<string>>; export const traverse: (name: string, showDirs?: boolean) => Promise<ReadonlyArray<string>>;
/** /**
* (async) Copy a directory * (async) Copy a directory
* Copy a folder with all the contained files * Copy a folder with all the contained files
*/ */
export const copyall: (src: string, dest: string) => Promise<void>; export const copyall: (src: string, dest: string) => Promise<void>;
/** /**
* (async) Remove a directory * (async) Remove a directory
* Like `rm -rf`, removes everything recursively * Like `rm -rf`, removes everything recursively
*/ */
export const rmdir: (name: string) => Promise<void>; export const rmdir: (name: string) => Promise<void>;
/** /**
* (async) Remove a file * (async) Remove a file
* Must be a file, not a folder. Just `fs.unlink`. * Must be a file, not a folder. Just `fs.unlink`.
*/ */
export const rm: (name: string) => Promise<void>; export const rm: (name: string) => Promise<void>;
} }

View File

@ -1,6 +1,10 @@
'use strict'; 'use strict';
const util = require('node:util');
const exec = util.promisify(require('node:child_process').exec);
const utils = require('.'); const utils = require('.');
const packageJson = require('../package.json');
describe('utils.js', () => { describe('utils.js', () => {
@ -12,8 +16,15 @@ describe('utils.js', () => {
]; ];
methods.forEach((name) => { methods.forEach((name) => {
it('is a function', () => { it(`exports the "${name}" function`, () => {
expect(typeof utils[name]).toBe('function'); expect(typeof utils[name]).toBe('function');
}); });
}); });
describe('actionVersion', () => {
it('logs package version', async () => {
const { stdout } = await exec('node -e "require(\'./utils\').actionVersion()"');
expect(stdout).toBe(`version=${packageJson.version}`);
});
});
}); });