codify-plugin-lib 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,52 @@
1
+ import { describe, it } from 'mocha';
2
+ import { EventEmitter } from 'node:events';
3
+ import { ChildProcess } from 'node:child_process';
4
+
5
+ import { Readable } from 'stream';
6
+ import { mock } from 'node:test';
7
+ import { expect } from '@oclif/test';
8
+ import { AssertionError } from 'chai';
9
+ import { CodifyTestUtils } from './test-utils';
10
+ import chai = require('chai');
11
+ import chaiAsPromised = require('chai-as-promised');
12
+
13
+ describe('Test Utils tests', async () => {
14
+
15
+ before(() => {
16
+ chai.use(chaiAsPromised);
17
+ })
18
+
19
+ const mockChildProcess = () => {
20
+ const process = new ChildProcess();
21
+ process.stdout = new EventEmitter() as Readable;
22
+ process.stderr = new EventEmitter() as Readable
23
+ process.send = () => true;
24
+
25
+ return process;
26
+ }
27
+
28
+ it('send a message', async () => {
29
+ const process = mockChildProcess();
30
+ const sendMock = mock.method(process, 'send');
31
+
32
+ CodifyTestUtils.sendMessageToProcessAwaitResponse(process, { cmd: 'message', data: 'data' })
33
+
34
+ expect(sendMock.mock.calls.length).to.eq(1);
35
+ expect(sendMock.mock.calls[0].arguments[0]).to.deep.eq({ cmd: 'message', data: 'data' });
36
+ })
37
+
38
+ it('send a message and receives the response', async () => {
39
+ const process = mockChildProcess();
40
+
41
+ try {
42
+ await Promise.all([
43
+ expect(CodifyTestUtils.sendMessageToProcessAwaitResponse(process, { cmd: 'message', data: 'data' }))
44
+ .to.eventually.deep.eq({ cmd: 'messageResult', data: 'data' }),
45
+ process.emit('message', { cmd: 'messageResult', data: 'data' }),
46
+ ]);
47
+ } catch (e) {
48
+ console.log(e);
49
+ throw new AssertionError('Failed to receive message');
50
+ }
51
+ });
52
+ });
@@ -0,0 +1,20 @@
1
+ import { ChildProcess } from 'child_process';
2
+
3
+ export class CodifyTestUtils {
4
+ static sendMessageToProcessAwaitResponse(process: ChildProcess, message: any): Promise<any> {
5
+ return new Promise((resolve, reject) => {
6
+ process.on('message', (response) => {
7
+ resolve(response)
8
+ });
9
+ process.on('error', (err) => reject(err))
10
+ process.on('exit', (code) => {
11
+ if (code != 0) {
12
+ reject('Exit code is not 0');
13
+ }
14
+ resolve(code);
15
+ })
16
+ process.send(message);
17
+ });
18
+ }
19
+
20
+ }
@@ -0,0 +1,33 @@
1
+ import promiseSpawn from '@npmcli/promise-spawn';
2
+ import { SpawnOptions } from 'child_process';
3
+
4
+ export interface SpawnResult {
5
+ code: number;
6
+ signal: NodeJS.Signals | null;
7
+ stdout: string;
8
+ stderr: string;
9
+ }
10
+
11
+ type CodifySpawnOptions = {
12
+ cwd?: string;
13
+ stdioString?: boolean;
14
+ } & SpawnOptions
15
+
16
+ export async function codifySpawn(
17
+ cmd: string,
18
+ args: string[],
19
+ opts?: CodifySpawnOptions,
20
+ extras?: Record<any, any>,
21
+ ): Promise<SpawnResult> {
22
+ const stdio = isDebug() ? 'inherit' : 'pipe';
23
+ return promiseSpawn(
24
+ cmd,
25
+ args,
26
+ { ...opts, stdio, stdioString: true },
27
+ extras
28
+ );
29
+ }
30
+
31
+ export function isDebug(): boolean {
32
+ return process.env.DEBUG != null && process.env.DEBUG.includes('codify');
33
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,25 @@
1
+ {
2
+ "compilerOptions": {
3
+ "declaration": true,
4
+ "module": "commonjs",
5
+ "alwaysStrict": true,
6
+ "noImplicitAny": true,
7
+ "resolveJsonModule": true,
8
+ "esModuleInterop": true,
9
+ "removeComments": true,
10
+ "strictNullChecks": true,
11
+ "emitDecoratorMetadata": true,
12
+ "experimentalDecorators": true,
13
+ "outDir": "dist",
14
+ "rootDir": "src",
15
+ "strict": true,
16
+ "target": "es2022"
17
+ },
18
+ "include": [
19
+ "src/**/*.ts"
20
+ ],
21
+ "exclude": [
22
+ "node_modules",
23
+ "src/**/*.test.ts"
24
+ ]
25
+ }
@@ -0,0 +1,9 @@
1
+ {
2
+ "extends": "tsconfig.json",
3
+ "compilerOptions": {
4
+ "strictNullChecks": false
5
+ },
6
+ "include": [
7
+ "src/**/*.test.ts"
8
+ ],
9
+ }