Skip to content

Commit a288183

Browse files
committed
add eslitn & prettier
1 parent 97ce3d1 commit a288183

File tree

6 files changed

+900
-28
lines changed

6 files changed

+900
-28
lines changed

.eslintignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# don't ever lint node_modules
2+
node_modules
3+
# don't lint build output (make sure it's set to your correct build folder name)
4+
dist
5+
# don't lint nyc coverage output
6+
coverage

.eslintrc.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module.exports = {
2+
root: true,
3+
env: { node: true },
4+
parser: "@typescript-eslint/parser",
5+
plugins: ["@typescript-eslint"],
6+
extends: [
7+
"eslint:recommended",
8+
"plugin:@typescript-eslint/eslint-recommended",
9+
"plugin:@typescript-eslint/recommended",
10+
"prettier",
11+
"prettier/@typescript-eslint"
12+
]
13+
};

.prettierrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"trailingComma": "es5",
3+
"singleQuote": true
4+
}

package.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
"watch-module": "bin/watch-module.js"
77
},
88
"scripts": {
9-
"build": "tsc --build tsconfig.json",
10-
"prepublishOnly": "tsc --build tsconfig.json"
9+
"lint": "eslint . --ext .js,.jsx,.ts,.tsx",
10+
"build": "tsc --build tsconfig.json",
11+
"prepublishOnly": "tsc --build tsconfig.json"
1112
},
1213
"repository": "https://github.com/mapado/watch-module",
1314
"license": "MIT",
@@ -24,6 +25,11 @@
2425
"@types/fs-extra": "^8.0.0",
2526
"@types/minimist": "^1.2.0",
2627
"@types/node": "^12.7.2",
28+
"@typescript-eslint/eslint-plugin": "^2.16.0",
29+
"@typescript-eslint/parser": "^2.16.0",
30+
"eslint": "^6.8.0",
31+
"eslint-config-prettier": "^6.9.0",
32+
"prettier": "^1.19.1",
2733
"typescript": "^3.5.3"
2834
}
2935
}

src/index.ts

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,39 +20,41 @@ enum Theme {
2020
// orange: '#fc9867',
2121
// purple: '#ab9df2',
2222
// cyan: '#78dce8',
23-
};
23+
}
2424

2525
/* ================== logging ================== */
2626
const cwd = nodeProcess.cwd();
27-
const argv = minimist(nodeProcess.argv.slice(2)).filter((a: string|undefined) => a);
27+
const argv = minimist(nodeProcess.argv.slice(2));
2828

2929
const logDate = () => chalk.hex(Theme.date)(`[${new Date().toISOString()}]`);
3030

31-
const debug = (...args: string[]) => {
31+
const debug = (...args: any[]) => {
3232
if (argv.v || argv.verbose) {
33-
console.debug(logDate(), chalk.hex(Theme.debug)('DEBUG'), ...args);
33+
console.debug(logDate(), chalk.hex(Theme.debug)('DEBUG'), ...args);
3434
}
35-
}
35+
};
3636

3737
const log = (...args: string[]) => {
38-
console.log(logDate(), ...args);
39-
}
38+
console.log(logDate(), ...args);
39+
};
4040

4141
const logModuleName = chalk.hex(Theme.moduleName);
4242

4343
debug('arguments: ', argv);
4444

45-
const moduleNameByPath: {[key:string]: string} = {};
45+
const moduleNameByPath: { [key: string]: string } = {};
4646
function getModuleNameForPath(path: string): string {
4747
if (!moduleNameByPath[path]) {
48-
moduleNameByPath[path] = require(`${cwd}/${path}/package.json`).name;
48+
moduleNameByPath[path] = require(`${cwd}/${path}/package.json`).name;
4949
}
5050

5151
return moduleNameByPath[path];
5252
}
5353

5454
function getModuleCommandForPath(path: string): string {
55-
const packageJson = require(`${cwd}/${path}/package.json`);
55+
const packageJson = JSON.parse(
56+
fs.readFileSync(`${cwd}/${path}/package.json`).toString()
57+
);
5658
if (packageJson['watch-module'] && packageJson['watch-module']['command']) {
5759
return packageJson['watch-module']['command'];
5860
}
@@ -64,12 +66,6 @@ function getModuleCommandForPath(path: string): string {
6466
}
6567

6668
/* ================== build ================== */
67-
const changedModules: Set<string> = new Set();
68-
function buildAll(): void {
69-
changedModules.forEach(buildPath);
70-
changedModules.clear();
71-
}
72-
7369
function buildPath(path: string): void {
7470
const moduleName = getModuleNameForPath(path);
7571
log(logModuleName(moduleName), `Change detected`);
@@ -92,25 +88,35 @@ function buildPath(path: string): void {
9288
}
9389
debug(`Create module directory for "${moduleName}" and copy files`);
9490
const modulePath = `${cwd}/node_modules/${moduleName}`;
95-
return fs.ensureDir(modulePath)
91+
return fs
92+
.ensureDir(modulePath)
9693
.then(() =>
9794
fs.copy(path, modulePath, {
98-
filter: (src: string, dest: string) => {
95+
filter: (src: string, dest: string) => {
9996
const srcAppendSlash = `${src}/`;
10097

10198
return (
10299
!srcAppendSlash.startsWith(`${path}/node_modules/`) &&
103100
!srcAppendSlash.startsWith(`${path}/.git/`)
104101
);
105-
}
102+
},
106103
})
107104
)
108105
.then(() => {
109-
log(logModuleName(moduleName), chalk.hex(Theme.success)('build done'));
106+
log(
107+
logModuleName(moduleName),
108+
chalk.hex(Theme.success)('build done')
109+
);
110110
})
111111
.catch(console.error);
112112
}
113-
)
113+
);
114+
}
115+
116+
const changedModules: Set<string> = new Set();
117+
function buildAll(): void {
118+
changedModules.forEach(buildPath);
119+
changedModules.clear();
114120
}
115121

116122
/* ================== debounce & events ================== */
@@ -134,9 +140,15 @@ function main(): void {
134140

135141
// One-liner for current directory, ignores .dotfiles
136142
chokidar
137-
.watch(srcPaths, { ignored: /(^|[\/\\])\.[^\.\/]/ })
143+
.watch(srcPaths, { ignored: /(^|[/\\])\.[^./]/ })
138144
.on('all', (event: any, path: string) => {
139-
const modulePath = modulePaths.find((tmpPath: string) => path.startsWith(`${tmpPath}/`));
145+
const modulePath = modulePaths.find((tmpPath: string) =>
146+
path.startsWith(`${tmpPath}/`)
147+
);
148+
149+
if (!modulePath) {
150+
throw new Error('Unable to find module path. This should not happen.');
151+
}
140152

141153
onChange(modulePath);
142154
});

0 commit comments

Comments
 (0)