-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli.test.ts
142 lines (113 loc) · 3.85 KB
/
cli.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import { describe, test, expect, beforeAll, afterAll } from "vitest";
import path from "node:path";
import fse from "fs-extra";
import child, { ChildProcessWithoutNullStreams } from "node:child_process";
import util from "node:util";
import concat from "concat-stream";
import { spawn } from "node:child_process";
const keys = {
ENTER: "\x0D",
};
// outside monorepo
const cwd = path.resolve(__dirname, "../../../..");
const testDir = "my-test" as const;
const exe = util.promisify(child.execFile);
const createCodes = path.resolve(__dirname, "../dist/index.js");
const EXPECTED_HELP = `Create a new codes for front-end app
Usage:
$ npx create-codes [<dir>] [flags...]
Flags:
--help, -h Show this help message
--version, -v Show the version of this script`;
describe("create-codes cli", () => {
beforeAll(() => cleanupTestDir());
afterAll(() => cleanupTestDir());
describe("install react boilerplate with cli", () => {
test("interactively configure", async () => {
const cli = spawn("node", [createCodes], { cwd });
const results = await exeInteractive(cli, [testDir, keys.ENTER]);
expect(results).toContain(`Create Codes`);
expect(results).toContain(`Welcome!`);
expect(results).toContain(`? Where would you like to create your app?`);
expect(results).toContain(`Success! Created a new app at "my-test".`);
});
});
describe("install react boilerplate to specify dir", () => {
test("install", async () => {
await exe("node", [createCodes, testDir]);
const expectDirs = [
".env.template",
".eslintrc.js",
".gitignore",
".npmrc",
"README.md",
"__mocks__",
"babel.config.js",
"index.html",
"jest-setup.ts",
"package.json",
"public",
"src",
"tests",
"tsconfig.json",
"vite.config.ts",
"yarn.lock",
];
expect(fse.readdirSync(testDir)).toStrictEqual(expectDirs);
});
});
describe("printing help message", () => {
test("--help flag works", async () => {
const { stdout } = await exe("node", [createCodes, "--help"]);
expect(stdout.trim()).toBe(EXPECTED_HELP);
});
test("-h flag works", async () => {
const { stdout } = await exe("node", [createCodes, "-h"]);
expect(stdout.trim()).toBe(EXPECTED_HELP);
});
});
describe("printing version", () => {
test("--version flag works", async () => {
const { stdout } = await exe("node", [createCodes, "--version"]);
// eslint-disable-next-line turbo/no-undeclared-env-vars
expect(stdout.trim()).toBe(process.env.npm_package_version);
});
test("-v flag works", async () => {
const { stdout } = await exe("node", [createCodes, "-v"]);
// eslint-disable-next-line turbo/no-undeclared-env-vars
expect(stdout.trim()).toBe(process.env.npm_package_version);
});
});
});
function cleanupTestDir() {
const installedTestDir = path.resolve(cwd, testDir);
fse.existsSync(installedTestDir) &&
fse.rmSync(installedTestDir, { recursive: true });
}
function exeInteractive(
cli: ChildProcessWithoutNullStreams,
inputs: string[] = [],
delay: number = 200
) {
let currentInputTimeout: NodeJS.Timeout;
cli.stdin.setDefaultEncoding("utf-8");
const loop = (inputs: string[]) => {
if (!inputs.length) return void cli.stdin.end();
currentInputTimeout = setTimeout(() => {
cli.stdin.write(inputs[0]);
loop(inputs.slice(1));
}, delay);
};
return new Promise((resolve, reject) => {
cli.stderr.once("data", (err) => {
cli.stdin.end();
if (currentInputTimeout) clearTimeout(currentInputTimeout);
reject(err.toString());
});
cli.on("error", reject);
loop(inputs);
cli.stdout.pipe(
concat((result: Buffer) => resolve(result.toString("utf-8")))
);
});
}