Skip to content

Commit 8c53321

Browse files
committed
init eslint prettier
1 parent 3e18660 commit 8c53321

15 files changed

+1494
-69
lines changed

.eslintignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
dist/
2+
old-code/
3+
dev/
4+
node_modules

.eslintrc.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
module.exports = {
2+
env: {
3+
es6: true,
4+
node: true,
5+
jest: true
6+
},
7+
extends: ['google', 'prettier'],
8+
plugins: ['prettier'],
9+
globals: {
10+
Atomics: 'readonly',
11+
SharedArrayBuffer: 'readonly'
12+
},
13+
parserOptions: {
14+
ecmaVersion: 2018
15+
},
16+
rules: {
17+
'prettier/prettier': ['error'],
18+
'comma-dangle': 0,
19+
'require-jsdoc': 0,
20+
'arrow-parens': 0,
21+
'guard-for-in': 0,
22+
'valid-jsdoc': 0,
23+
'no-undef': 'error',
24+
'operator-linebreak': [
25+
'error',
26+
'after',
27+
{
28+
overrides: { '?': 'ignore', ':': 'ignore', '+': 'ignore' }
29+
}
30+
],
31+
indent: [
32+
'error',
33+
2,
34+
{
35+
CallExpression: { arguments: 'first' },
36+
ignoredNodes: [
37+
'CallExpression > CallExpression',
38+
'CallExpression > MemberExpression'
39+
],
40+
SwitchCase: 1
41+
}
42+
],
43+
'max-len': ['error', { code: 80, ignoreComments: true }]
44+
}
45+
};

.prettierignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
dist/
2+
old-code/
3+
dev/

.prettierrc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"semi": true,
3+
"singleQuote": true,
4+
"printWidth": 80,
5+
"trailingComma": "none"
6+
}

cli-examples/__tests__/multiple-prompts.test.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
1-
const path = require("path");
2-
const { createCommandInterface } = require("../../lib");
1+
const path = require('path');
2+
const { createCommandInterface } = require('../../lib');
33

4-
const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
5-
6-
test("should pass", async () => {
4+
test('should pass', async () => {
75
const commandInterface = createCommandInterface(
8-
"node ./multiple-prompts.js",
6+
'node ./multiple-prompts.js',
97
{
10-
cwd: path.join(__dirname, ".."),
11-
logData: true,
8+
cwd: path.join(__dirname, '..'),
9+
logData: true
1210
}
1311
);
14-
await commandInterface.type("19\n");
12+
await commandInterface.type('19\n');
1513
// await wait(100);
16-
await commandInterface.type("saurabh\n");
14+
await commandInterface.type('saurabh\n');
1715
const terminal = await commandInterface.getOutput();
1816
console.log(terminal);
1917
expect(1).toBe(1);
Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
const path = require("path");
2-
const { createCommandInterface } = require("../../lib");
1+
const path = require('path');
2+
const { createCommandInterface } = require('../../lib');
33

44
// Windows is just weird.
5-
const FORWARD_ARROW = process.platform === "win32" ? "»" : "›";
5+
const FORWARD_ARROW = process.platform === 'win32' ? '»' : '›';
66

7-
test("should block 12 year old", async () => {
8-
const commandInterface = createCommandInterface("node ./prompts.js", {
9-
cwd: path.join(__dirname, ".."),
7+
test('should block 12 year old', async () => {
8+
const commandInterface = createCommandInterface('node ./prompts.js', {
9+
cwd: path.join(__dirname, '..')
1010
});
11-
await commandInterface.type("12\n");
11+
await commandInterface.type('12\n');
1212
const terminal = await commandInterface.getOutput();
1313
expect(terminal.stringOutput).toBe(
14+
// eslint-disable-next-line max-len
1415
`? How old are you? ${FORWARD_ARROW} 12\n${FORWARD_ARROW} Nightclub is 18+ only`
1516
);
1617
});
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
const path = require("path");
2-
const { createCommandInterface } = require("../../lib");
1+
const path = require('path');
2+
const { createCommandInterface } = require('../../lib');
33

4-
test("should pass", async () => {
5-
const commandInterface = createCommandInterface("node ./simple-print.js", {
6-
cwd: path.join(__dirname, ".."),
4+
test('should pass', async () => {
5+
const commandInterface = createCommandInterface('node ./simple-print.js', {
6+
cwd: path.join(__dirname, '..')
77
});
8-
await commandInterface.type("Saurabh\n");
9-
await commandInterface.type("22\n");
8+
await commandInterface.type('Saurabh\n');
9+
await commandInterface.type('22\n');
1010
const terminal = await commandInterface.getOutput();
1111

12-
expect(terminal.textAfter("What's your name?")).toBe("Hi, Saurabh!"); // Hi, Saurabh!
13-
expect(terminal.textAfter("What's your age?")).toBe("So you are 22!"); // So you are 22!
12+
expect(terminal.textAfter("What's your name?")).toBe('Hi, Saurabh!'); // Hi, Saurabh!
13+
expect(terminal.textAfter("What's your age?")).toBe('So you are 22!'); // So you are 22!
1414
});

cli-examples/multiple-prompts.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
const prompts = require("prompts");
1+
const prompts = require('prompts');
22

33
(async () => {
44
const response1 = await prompts({
5-
type: "number",
6-
name: "value",
7-
message: "How old are you?",
8-
validate: (value) => (value < 18 ? `Nightclub is 18+ only` : true),
5+
type: 'number',
6+
name: 'value',
7+
message: 'How old are you?',
8+
validate: (value) => (value < 18 ? `Nightclub is 18+ only` : true)
99
});
1010

1111
console.log(response1); // => { value: 24 }
1212

1313
const response2 = await prompts({
14-
type: "text",
15-
name: "value",
16-
message: "What is your name?",
14+
type: 'text',
15+
name: 'value',
16+
message: 'What is your name?'
1717
});
1818

1919
console.log(response2);

cli-examples/prompts.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
const prompts = require("prompts");
1+
const prompts = require('prompts');
22

33
(async () => {
44
const response = await prompts({
5-
type: "number",
6-
name: "value",
7-
message: "How old are you?",
8-
validate: (value) => (value < 18 ? `Nightclub is 18+ only` : true),
5+
type: 'number',
6+
name: 'value',
7+
message: 'How old are you?',
8+
validate: (value) => (value < 18 ? `Nightclub is 18+ only` : true)
99
});
1010

1111
console.log(response); // => { value: 24 }

cli-examples/simple-print.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
const readline = require("readline").createInterface({
1+
const readline = require('readline').createInterface({
22
input: process.stdin,
3-
output: process.stdout,
3+
output: process.stdout
44
});
55

66
readline.question(`What's your name?`, (name) => {

lib/cli-ansi-parser.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
*/
1717

1818
// Big shoutout to https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797 for super cool reference to all ansi codes
19-
const ESC = "(?:\\x1[bB])|(?:\\u001b)\\[";
20-
const ESC_NO_BRACKET = ESC.replace("\\[", "");
21-
const STRING_ESC = "\x1b[";
19+
const ESC = '(?:\\x1[bB])|(?:\\u001b)\\[';
20+
const ESC_NO_BRACKET = ESC.replace('\\[', '');
21+
const STRING_ESC = '\x1b[';
2222
const ANSI_CHARACTER_MAPS = {
2323
// Cursor and Line erase
2424
ERASE_LINE: `${ESC}2K`,
@@ -43,13 +43,13 @@ const ANSI_CHARACTER_MAPS = {
4343
ITALIC_START: `${ESC}3m`,
4444
ITALIC_END: `${ESC}23m`,
4545
UNDERLINE_START: `${ESC}4m`,
46-
UNDERLINE_END: `${ESC}24m`,
46+
UNDERLINE_END: `${ESC}24m`
4747
};
4848

4949
const parseOutput = (output) => {
5050
const textAfter = (question) => {
5151
const questionIndex = output.indexOf(question) + question.length;
52-
const endlineIndex = output.indexOf("\n", questionIndex + 1);
52+
const endlineIndex = output.indexOf('\n', questionIndex + 1);
5353
const cleanEndlineIndex = endlineIndex <= 0 ? undefined : endlineIndex;
5454
return output.slice(questionIndex, cleanEndlineIndex).trim();
5555
};
@@ -58,10 +58,10 @@ const parseOutput = (output) => {
5858
let out = output;
5959
for (const [
6060
ESCAPE_CHARACTER_NAME,
61-
ESCAPE_CHARACTER_REGEX,
61+
ESCAPE_CHARACTER_REGEX
6262
] of Object.entries(ANSI_CHARACTER_MAPS)) {
6363
out = out.replace(
64-
new RegExp(`(${ESCAPE_CHARACTER_REGEX})`, "g"),
64+
new RegExp(`(${ESCAPE_CHARACTER_REGEX})`, 'g'),
6565
`[${ESCAPE_CHARACTER_NAME}]`
6666
);
6767
}
@@ -70,19 +70,19 @@ const parseOutput = (output) => {
7070
};
7171

7272
const finalString = () => {
73-
let parsedOutput = tokenizeOutput();
74-
const lastEraseLineIndex = parsedOutput.lastIndexOf("[ERASE_LINE]");
73+
const parsedOutput = tokenizeOutput();
74+
const lastEraseLineIndex = parsedOutput.lastIndexOf('[ERASE_LINE]');
7575
const outputAfterLastEraseLine = parsedOutput.slice(
7676
lastEraseLineIndex > 0 ? lastEraseLineIndex : 0
7777
);
78-
return outputAfterLastEraseLine.replace(/\[(\w*)\]/g, "");
78+
return outputAfterLastEraseLine.replace(/\[(\w*)\]/g, '');
7979
};
8080

8181
return {
8282
textAfter,
8383
rawOutput: output,
8484
tokenizedOutput: tokenizeOutput(),
85-
stringOutput: finalString(),
85+
stringOutput: finalString()
8686
};
8787
};
8888

lib/cli-testing-library.js

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const { spawn } = require("child_process");
2-
const { parseOutput } = require("./cli-ansi-parser");
1+
const { spawn } = require('child_process');
2+
const { parseOutput, STRING_ESC } = require('./cli-ansi-parser');
33

44
const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
55

@@ -11,39 +11,40 @@ const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
1111
const createCommandInterface = (commandString, options) => {
1212
// /** @type {import('child_process').ChildProcessWithoutNullStreams} */
1313
// let command;
14-
const commandArgs = commandString.split(" ");
14+
const commandArgs = commandString.split(' ');
1515
const command = spawn(commandArgs[0], commandArgs.slice(1), {
1616
detached: true,
17-
stdio: "pipe",
17+
stdio: 'pipe',
1818
cwd: options.cwd || process.cwd(),
19-
env: options.env || undefined,
19+
env: options.env || undefined
2020
});
2121

22-
let outputs = "";
22+
let outputs = '';
2323
let isFinishTypingCalled = false;
2424

25-
command.stdout.on("data", (data) => {
25+
command.stdout.on('data', (data) => {
2626
if (options.logData) {
2727
console.log(data.toString());
2828
}
2929
outputs += data.toString();
3030
});
3131

32-
command.stderr.on("data", (error) => {
32+
command.stderr.on('data', (error) => {
3333
if (options.logData) {
3434
console.error(error.toString());
3535
}
3636
outputs += error.toString();
3737
});
3838

39-
command.on("error", (error) => {
39+
command.on('error', (error) => {
4040
throw error;
4141
});
4242

4343
const type = async (text) => {
4444
if (isFinishTypingCalled) {
4545
throw new Error(
46-
"[cli-testing-library]: `type` cannot be called after `getOutput` or `finishTyping`"
46+
// eslint-disable-next-line max-len
47+
'[cli-testing-library]: `type` cannot be called after `getOutput` or `finishTyping`'
4748
);
4849
}
4950

@@ -57,9 +58,9 @@ const createCommandInterface = (commandString, options) => {
5758
};
5859

5960
const keys = {
60-
enter: () => type("\n"),
61+
enter: () => type('\n'),
6162
arrowDown: () => type(`${STRING_ESC}1B`),
62-
arrowUp: () => type(`${STRING_ESC}1A`),
63+
arrowUp: () => type(`${STRING_ESC}1A`)
6364
};
6465

6566
/**
@@ -70,7 +71,7 @@ const createCommandInterface = (commandString, options) => {
7071
finishTyping();
7172
}
7273
return new Promise((resolve) => {
73-
command.stdout.on("end", () => {
74+
command.stdout.on('end', () => {
7475
return resolve(parseOutput(outputs.trim()));
7576
});
7677
});
@@ -86,11 +87,11 @@ const createCommandInterface = (commandString, options) => {
8687
finishTyping,
8788
getOutput,
8889
command,
89-
keys,
90+
keys
9091
};
9192
};
9293

9394
module.exports = {
9495
createCommandInterface,
95-
parseOutput,
96+
parseOutput
9697
};

lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
module.exports = require('./cli-testing-library');
1+
module.exports = require('./cli-testing-library');

0 commit comments

Comments
 (0)