Skip to content

Commit 586bbe2

Browse files
committed
feat: also lint & prettier compiler stuff
1 parent 84e6aeb commit 586bbe2

File tree

7 files changed

+126
-154
lines changed

7 files changed

+126
-154
lines changed

compiler/cli.ts

+29-32
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,37 @@
1-
import inquirer from 'inquirer'
2-
import Logger, { Level } from './log'
3-
import fs from 'fs'
4-
import { getBoilerplateSpecContent, SOURCE_FOLDER_NAME } from './constants'
5-
import path from 'path'
1+
import inquirer from 'inquirer';
2+
import Logger, { Level } from './log';
3+
import fs from 'fs';
4+
import { getBoilerplateSpecContent, SOURCE_FOLDER_NAME } from './constants';
5+
import path from 'path';
66

77
async function createNewSpecCLI() {
8-
try {
9-
const { cliName } = await inquirer.prompt<{ cliName: string }>([
10-
{
11-
type: 'input',
12-
message: "What's the name of the CLI you want to create the Spec for?",
13-
name: 'cliName',
14-
},
15-
])
8+
try {
9+
const { cliName } = await inquirer.prompt<{ cliName: string }>([
10+
{
11+
type: 'input',
12+
message: "What's the name of the CLI you want to create the Spec for?",
13+
name: 'cliName',
14+
},
15+
]);
1616

17-
const specFileName = `${cliName.toLowerCase()}.ts`
18-
const specPath = path.join(process.cwd(), SOURCE_FOLDER_NAME, specFileName)
19-
const hasSpec = fs.existsSync(specPath)
17+
const specFileName = `${cliName.toLowerCase()}.ts`;
18+
const specPath = path.join(process.cwd(), SOURCE_FOLDER_NAME, specFileName);
19+
const hasSpec = fs.existsSync(specPath);
2020

21-
// We don't want to overwrite the spec if it already exists
22-
if (hasSpec) {
23-
Logger.log(`The spec "${specFileName}" already exists.`, Level.ERROR)
24-
return
25-
}
21+
// We don't want to overwrite the spec if it already exists
22+
if (hasSpec) {
23+
Logger.log(`The spec "${specFileName}" already exists.`, Level.ERROR);
24+
return;
25+
}
2626

27-
fs.writeFileSync(specPath, getBoilerplateSpecContent(cliName), {
28-
encoding: 'utf-8',
29-
})
27+
fs.writeFileSync(specPath, getBoilerplateSpecContent(cliName), {
28+
encoding: 'utf-8',
29+
});
3030

31-
Logger.log(
32-
`Successfully created the new Spec "${specFileName}"!`,
33-
Level.SUCCESS
34-
)
35-
} catch (e) {
36-
Logger.log("Couldn't create Spec! Please try again.", Level.ERROR)
37-
}
31+
Logger.log(`Successfully created the new Spec "${specFileName}"!`, Level.SUCCESS);
32+
} catch (e) {
33+
Logger.log("Couldn't create Spec! Please try again.", Level.ERROR);
34+
}
3835
}
3936

40-
createNewSpecCLI()
37+
createNewSpecCLI();

compiler/compiler.ts

+47-53
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,70 @@
1-
import fs from 'fs'
2-
import ts from 'typescript'
3-
import path from 'path'
4-
import { specTransformer } from './transformer'
5-
import SpecLogger, { Level } from './log'
6-
import ProgressBar from 'progress'
7-
import { DESTINATION_FOLDER_NAME, SOURCE_FOLDER_NAME } from './constants'
1+
import fs from 'fs';
2+
import ts from 'typescript';
3+
import path from 'path';
4+
import { specTransformer } from './transformer';
5+
import SpecLogger, { Level } from './log';
6+
import ProgressBar from 'progress';
7+
import { DESTINATION_FOLDER_NAME, SOURCE_FOLDER_NAME } from './constants';
88

99
// The options for the TypeScript compiler
1010
const options: ts.TranspileOptions = {
11-
compilerOptions: {
12-
module: ts.ModuleKind.ESNext,
13-
},
14-
transformers: {
15-
before: [specTransformer],
16-
},
17-
}
11+
compilerOptions: {
12+
module: ts.ModuleKind.ESNext,
13+
},
14+
transformers: {
15+
before: [specTransformer],
16+
},
17+
};
1818

1919
/**
2020
* Process a spec by transpiling it with the TypeScript
2121
* compiler.
2222
* @param file The file to process
2323
*/
2424
const processSpec = (file: string) => {
25-
const source = fs.readFileSync(file).toString()
26-
const result = ts.transpileModule(source, options)
25+
const source = fs.readFileSync(file).toString();
26+
const result = ts.transpileModule(source, options);
2727

28-
let newName = path.basename(file, '.ts')
28+
let newName = path.basename(file, '.ts');
2929

30-
if (!newName.endsWith('.js')) {
31-
newName += '.js'
32-
}
30+
if (!newName.endsWith('.js')) {
31+
newName += '.js';
32+
}
3333

34-
const outFilePath = path.resolve(DESTINATION_FOLDER_NAME, newName)
35-
const outDirname = path.dirname(outFilePath)
34+
const outFilePath = path.resolve(DESTINATION_FOLDER_NAME, newName);
35+
const outDirname = path.dirname(outFilePath);
3636

37-
if (!fs.existsSync(outDirname)) {
38-
fs.mkdirSync(outDirname)
39-
}
37+
if (!fs.existsSync(outDirname)) {
38+
fs.mkdirSync(outDirname);
39+
}
4040

41-
// Remove unessesary export at the end of js files
42-
const jsOutput = result.outputText.replace("export {};", "")
41+
// Remove unessesary export at the end of js files
42+
const jsOutput = result.outputText.replace('export {};', '');
4343

44-
fs.writeFileSync(outFilePath, jsOutput)
45-
}
44+
fs.writeFileSync(outFilePath, jsOutput);
45+
};
4646

4747
// Process all the files in the specs directory
4848
fs.readdir(SOURCE_FOLDER_NAME, (err, files) => {
49-
if (err) {
50-
SpecLogger.log(
51-
`Could not find /${DESTINATION_FOLDER_NAME} folder`,
52-
Level.ERROR
53-
)
54-
return
55-
}
49+
if (err) {
50+
SpecLogger.log(`Could not find /${DESTINATION_FOLDER_NAME} folder`, Level.ERROR);
51+
return;
52+
}
5653

57-
const specs = files.filter((file) => file !== '.DS_STORE')
58-
SpecLogger.log(`Processing ${specs.length} specs...`)
54+
const specs = files.filter((file) => file !== '.DS_STORE');
55+
SpecLogger.log(`Processing ${specs.length} specs...`);
5956

60-
const bar = new ProgressBar(':bar :percent', {
61-
total: specs.length,
62-
complete: '=',
63-
head: '>',
64-
incomplete: ' ',
65-
})
57+
const bar = new ProgressBar(':bar :percent', {
58+
total: specs.length,
59+
complete: '=',
60+
head: '>',
61+
incomplete: ' ',
62+
});
6663

67-
specs.forEach((spec) => {
68-
processSpec(path.join(SOURCE_FOLDER_NAME, spec))
69-
bar.tick({ spec })
70-
})
64+
specs.forEach((spec) => {
65+
processSpec(path.join(SOURCE_FOLDER_NAME, spec));
66+
bar.tick({ spec });
67+
});
7168

72-
SpecLogger.log(
73-
`Specs compiled successfully to /${DESTINATION_FOLDER_NAME} folder!`,
74-
Level.SUCCESS
75-
)
76-
})
69+
SpecLogger.log(`Specs compiled successfully to /${DESTINATION_FOLDER_NAME} folder!`, Level.SUCCESS);
70+
});

compiler/constants.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
// Folder names
2-
export const SOURCE_FOLDER_NAME = 'dev'
3-
export const DESTINATION_FOLDER_NAME = 'specs'
4-
2+
export const SOURCE_FOLDER_NAME = 'dev';
3+
export const DESTINATION_FOLDER_NAME = 'specs';
54

65
export const getBoilerplateSpecContent = (specName: string) => `
76
export const completion: Fig.Spec = {
87
name: "${specName}"
9-
}`
8+
}`;

compiler/log.ts

-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import chalk from 'chalk';
33
// An enum representing the log level and the color to use
44
// in chalk to render this level.
55
export enum Level {
6-
76
INFO = 'blue',
87
ERROR = 'red',
98
SUCCESS = 'green',
@@ -24,9 +23,7 @@ export enum Level {
2423
* @param level The level to log
2524
*/
2625
export default abstract class SpecLogger {
27-
2826
static log(message: string, level: Level = Level.INFO) {
29-
3027
console.log(chalk[level](message));
3128
}
3229
}

compiler/transformer.ts

+42-49
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
import {
2-
factory,
3-
isVariableStatement,
4-
Node,
5-
SourceFile,
6-
SyntaxKind,
7-
TransformerFactory,
8-
updateVariableDeclaration,
9-
updateVariableDeclarationList,
10-
updateVariableStatement,
11-
VariableStatement,
12-
visitEachChild,
13-
visitNode,
14-
} from 'typescript'
2+
factory,
3+
isVariableStatement,
4+
Node,
5+
SourceFile,
6+
SyntaxKind,
7+
TransformerFactory,
8+
updateVariableDeclaration,
9+
updateVariableDeclarationList,
10+
updateVariableStatement,
11+
VariableStatement,
12+
visitEachChild,
13+
visitNode,
14+
} from 'typescript';
1515

1616
// The name of the spec variable
17-
const SPEC_NAME = 'completionSpec'
18-
const EXPORT_KEYWORD = SyntaxKind.ExportKeyword
17+
const SPEC_NAME = 'completionSpec';
18+
const EXPORT_KEYWORD = SyntaxKind.ExportKeyword;
1919

2020
/**
2121
* The TypeScript transformer to rename the export spec variable name to SPEC_NAME,
@@ -24,40 +24,33 @@ const EXPORT_KEYWORD = SyntaxKind.ExportKeyword
2424
* @param context The current context
2525
*/
2626
export const specTransformer: TransformerFactory<SourceFile> = (context) => {
27-
return (sourceFile) => {
28-
let updated = false
29-
const visitor = (node: Node) => {
30-
if (!updated && isVariableStatement(node)) {
31-
const { declarationList, modifiers } = node as VariableStatement
32-
// Only process if there is a modifier, and if this modifier
33-
// is an export.
34-
if (
35-
modifiers &&
36-
modifiers.length === 1 &&
37-
modifiers[0].kind === EXPORT_KEYWORD
38-
) {
39-
const variableNode = declarationList.declarations[0]
27+
return (sourceFile) => {
28+
let updated = false;
29+
const visitor = (node: Node) => {
30+
if (!updated && isVariableStatement(node)) {
31+
const { declarationList, modifiers } = node as VariableStatement;
32+
// Only process if there is a modifier, and if this modifier
33+
// is an export.
34+
if (modifiers && modifiers.length === 1 && modifiers[0].kind === EXPORT_KEYWORD) {
35+
const variableNode = declarationList.declarations[0];
4036

41-
updated = true
37+
updated = true;
4238

43-
// Update the variable name to SPEC_NAME
44-
const newVariableNode = updateVariableDeclaration(
45-
variableNode,
46-
factory.createIdentifier(SPEC_NAME),
47-
variableNode.type,
48-
variableNode.initializer
49-
)
50-
const newDeclarationlist = updateVariableDeclarationList(
51-
declarationList,
52-
[newVariableNode]
53-
)
39+
// Update the variable name to SPEC_NAME
40+
const newVariableNode = updateVariableDeclaration(
41+
variableNode,
42+
factory.createIdentifier(SPEC_NAME),
43+
variableNode.type,
44+
variableNode.initializer,
45+
);
46+
const newDeclarationlist = updateVariableDeclarationList(declarationList, [newVariableNode]);
5447

55-
// Remove the export keyword
56-
return updateVariableStatement(node, [], newDeclarationlist)
57-
}
58-
}
59-
return visitEachChild(node, visitor, context)
60-
}
61-
return visitNode(sourceFile, visitor)
62-
}
63-
}
48+
// Remove the export keyword
49+
return updateVariableStatement(node, [], newDeclarationlist);
50+
}
51+
}
52+
return visitEachChild(node, visitor, context);
53+
};
54+
return visitNode(sourceFile, visitor);
55+
};
56+
};

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"copy:all": "cp ./specs/*.js ~/.fig/autocomplete/",
1010
"watch": "ts-node-dev --respawn -s --watch ./dev/ compiler/compiler.ts",
1111
"create-boilerplate": "ts-node-script compiler/cli.ts",
12-
"lint": "eslint './dev/*.ts'",
12+
"lint": "eslint '**/*.ts'",
1313
"lint:fix": "npm run lint -- --fix"
1414
},
1515
"husky": {

0 commit comments

Comments
 (0)