Skip to content

Commit c2b6379

Browse files
committed
Introduce eslint and prettier
1 parent 4ba1afe commit c2b6379

20 files changed

+875
-298
lines changed

.eslintrc.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module.exports = {
2+
env: {
3+
browser: true,
4+
es6: true,
5+
node: true
6+
},
7+
extends: ['standard'],
8+
globals: {
9+
CONFIG: 'readonly',
10+
Atomics: 'readonly',
11+
SharedArrayBuffer: 'readonly'
12+
},
13+
parserOptions: {
14+
ecmaVersion: 2018,
15+
sourceType: 'module'
16+
},
17+
rules: {
18+
semi: 'off',
19+
'space-before-function-paren': 'off'
20+
}
21+
}

.prettierrc.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
trailingComma: 'none',
3+
tabWidth: 2,
4+
semi: false,
5+
singleQuote: true,
6+
}

package.json

+8
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"main": "lib/index.js",
1010
"scripts": {
1111
"build": "npx webpack",
12+
"lint": "npx eslint src",
1213
"test": "npx mocha test/lib --recursive --require coffeescript/register --extension coffee"
1314
},
1415
"keywords": [
@@ -51,8 +52,15 @@
5152
"babel-loader": "^8.1.0",
5253
"chai": "^4.2.0",
5354
"coffeescript": "^2.5.1",
55+
"eslint": "^6.8.0",
56+
"eslint-config-standard": "^14.1.1",
57+
"eslint-plugin-import": "^2.20.1",
58+
"eslint-plugin-node": "^11.0.0",
59+
"eslint-plugin-promise": "^4.2.1",
60+
"eslint-plugin-standard": "^4.0.1",
5461
"mocha": "^7.1.1",
5562
"mock-require": "^3.0.3",
63+
"prettier": "2.0.2",
5664
"require-reload": "^0.2.2",
5765
"sinon": "^9.0.1",
5866
"webpack-cli": "^3.3.11",

src/building/builder.js

+88-88
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,69 @@
1-
const fs = require("fs");
2-
const path = require("path");
3-
const ora = require("ora");
4-
const copy = require("copy");
5-
const chalk = require("chalk");
6-
const webpack = require("webpack");
7-
const chokidar = require("chokidar");
8-
const bozon = require("../utils/bozon");
9-
const WebpackConfig = require("./webpack");
1+
const fs = require('fs')
2+
const path = require('path')
3+
const ora = require('ora')
4+
const copy = require('copy')
5+
const chalk = require('chalk')
6+
const webpack = require('webpack')
7+
const chokidar = require('chokidar')
8+
const bozon = require('../utils/bozon')
9+
const WebpackConfig = require('./webpack')
1010

1111
class Builder {
1212
constructor(platform, environment) {
13-
this.platform = platform;
14-
this.environment = environment;
15-
this.config = new WebpackConfig(this.environment, this.platform).build();
13+
this.platform = platform
14+
this.environment = environment
15+
this.config = new WebpackConfig(this.environment, this.platform).build()
1616
this.spinner = ora({
17-
text: chalk.cyan("Building Electron application"),
18-
color: "cyan"
19-
});
17+
text: chalk.cyan('Building Electron application'),
18+
color: 'cyan'
19+
})
2020
}
2121

2222
run() {
23-
this.spinner.start();
23+
this.spinner.start()
2424
return Promise.all(this.buildQueue())
2525
.then(() => {
26-
this.manifest();
27-
this.spinner.succeed();
28-
if (this.environment === "development") {
29-
this.watch();
26+
this.manifest()
27+
this.spinner.succeed()
28+
if (this.environment === 'development') {
29+
this.watch()
3030
}
3131
})
32-
.catch(error => {
33-
this.spinner.fail("Failed to build application");
34-
console.error(error);
35-
});
32+
.catch((error) => {
33+
this.spinner.fail('Failed to build application')
34+
console.error(error)
35+
})
3636
}
3737

3838
buildQueue() {
39-
let queue = [
40-
this.copy("renderer/*.html", "renderer"),
41-
this.copy("images/**/*", "images"),
39+
const queue = [
40+
this.copy('renderer/*.html', 'renderer'),
41+
this.copy('images/**/*', 'images'),
4242
this.bundle(this.config.renderer),
4343
this.bundle(this.config.main)
44-
];
45-
if (this.config.preload) queue.push(this.bundle(this.config.preload));
46-
return queue;
44+
]
45+
if (this.config.preload) queue.push(this.bundle(this.config.preload))
46+
return queue
4747
}
4848

4949
bundle(config, callback) {
5050
return new Promise((resolve, reject) => {
5151
webpack(config, (error, stats) => {
5252
if (error || stats.hasErrors()) {
53-
this.spinner.fail(chalk.red("Webpack failed to bundle application"));
54-
bozon.log(stats.compilation.errors);
55-
return reject(error);
53+
this.spinner.fail(chalk.red('Webpack failed to bundle application'))
54+
bozon.log(stats.compilation.errors)
55+
return reject(error)
5656
}
5757
if (stats.compilation.warnings.length) {
5858
this.spinner.fail(
59-
chalk.yellow("Webpack bundled application with warnings")
60-
);
61-
bozon.log(stats.compilation.warnings);
59+
chalk.yellow('Webpack bundled application with warnings')
60+
)
61+
bozon.log(stats.compilation.warnings)
6262
}
63-
if (callback) callback();
64-
return resolve();
65-
});
66-
});
63+
if (callback) callback()
64+
return resolve()
65+
})
66+
})
6767
}
6868

6969
copy(input, output, callback) {
@@ -73,93 +73,93 @@ class Builder {
7373
bozon.destinationPath(output, this.environment),
7474
(error, file) => {
7575
if (error) {
76-
return reject(error);
76+
return reject(error)
7777
} else {
78-
if (callback) callback();
79-
return resolve();
78+
if (callback) callback()
79+
return resolve()
8080
}
8181
}
82-
);
83-
});
82+
)
83+
})
8484
}
8585

8686
manifest() {
8787
return new Promise((resolve, reject) => {
88-
const json = JSON.parse(fs.readFileSync(bozon.source("package.json")));
89-
let settings = {
88+
const json = JSON.parse(fs.readFileSync(bozon.source('package.json')))
89+
const settings = {
9090
name: json.name,
9191
version: json.version,
9292
description: json.description,
93-
author: json.author || "Anonymous",
94-
main: "main/index.js",
93+
author: json.author || 'Anonymous',
94+
main: 'main/index.js',
9595
repository: json.repository
96-
};
96+
}
9797
fs.writeFile(
98-
bozon.destinationPath("package.json", this.environment),
98+
bozon.destinationPath('package.json', this.environment),
9999
JSON.stringify(settings),
100-
err => {
100+
(err) => {
101101
if (err) {
102-
reject(err);
102+
reject(err)
103103
} else {
104-
resolve();
104+
resolve()
105105
}
106106
}
107-
);
108-
});
107+
)
108+
})
109109
}
110110

111111
watch() {
112-
let watcher = chokidar.watch(bozon.sourcePath("**/*.*"), {
112+
const watcher = chokidar.watch(bozon.sourcePath('**/*.*'), {
113113
ignored: /node_modules/,
114114
persistent: true
115-
});
116-
watcher.on("ready", () => {
115+
})
116+
watcher.on('ready', () => {
117117
this.spinner.stopAndPersist({
118-
text: chalk.green("Watching for changes.."),
119-
symbol: "👀"
120-
});
121-
});
122-
watcher.on("change", (file, stats) => {
118+
text: chalk.green('Watching for changes..'),
119+
symbol: '👀'
120+
})
121+
})
122+
watcher.on('change', (file, stats) => {
123123
this.log(
124-
chalk.green("CHANGE"),
124+
chalk.green('CHANGE'),
125125
`File '${chalk.yellow(
126126
path.relative(bozon.source(), file)
127127
)}' has been changed`
128-
);
128+
)
129129
if (file.match(/main/)) {
130-
let key = chalk.grey("~MAIN~");
131-
this.log(key, "Compiling..");
132-
this.bundle(this.config.main, () => this.log(key, chalk.cyan("Done")));
130+
const key = chalk.grey('~MAIN~')
131+
this.log(key, 'Compiling..')
132+
this.bundle(this.config.main, () => this.log(key, chalk.cyan('Done')))
133133
} else if (file.match(/\.html/)) {
134-
let key = chalk.grey("RENDER");
135-
this.log(key, "Updating..");
136-
this.copy("*.html", "", () => this.log(key, chalk.cyan("Done")));
134+
const key = chalk.grey('RENDER')
135+
this.log(key, 'Updating..')
136+
this.copy('*.html', '', () => this.log(key, chalk.cyan('Done')))
137137
} else if (file.match(/\.json/)) {
138-
let key = chalk.grey("RENDER");
139-
this.log(key, "Updating..");
140-
this.copy("*.json", "", () => this.log(key, chalk.cyan("Done")));
138+
const key = chalk.grey('RENDER')
139+
this.log(key, 'Updating..')
140+
this.copy('*.json', '', () => this.log(key, chalk.cyan('Done')))
141141
} else {
142-
let key = chalk.grey("RENDER");
143-
this.log(key, "Compiling..");
142+
const key = chalk.grey('RENDER')
143+
this.log(key, 'Compiling..')
144144
this.bundle(this.config.renderer, () =>
145-
this.log(key, chalk.cyan("Done"))
146-
);
145+
this.log(key, chalk.cyan('Done'))
146+
)
147147
}
148-
});
148+
})
149149
}
150150

151151
log(key, message) {
152-
let time = this.timestamp();
153-
bozon.log(`[${chalk.grey(time)}] [${key}] ${message}`);
152+
const time = this.timestamp()
153+
bozon.log(`[${chalk.grey(time)}] [${key}] ${message}`)
154154
}
155155

156156
timestamp() {
157-
let date = new Date();
158-
let hours = String(date.getHours()).padStart(2, "0");
159-
let minutes = String(date.getMinutes()).padStart(2, "0");
160-
let seconds = String(date.getSeconds()).padStart(2, "0");
161-
return `${hours}:${minutes}:${seconds}`;
157+
const date = new Date()
158+
const hours = String(date.getHours()).padStart(2, '0')
159+
const minutes = String(date.getMinutes()).padStart(2, '0')
160+
const seconds = String(date.getSeconds()).padStart(2, '0')
161+
return `${hours}:${minutes}:${seconds}`
162162
}
163163
}
164164

165-
module.exports = Builder;
165+
module.exports = Builder

src/building/webpack/index.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const mainDefault = require('./main');
55
const rendererDefault = require('./renderer');
66
const preloadDefault = require('./preload');
77

8-
98
class WebpackConfig {
109
constructor(env, platform) {
1110
this.env = env;
@@ -17,7 +16,7 @@ class WebpackConfig {
1716
}
1817

1918
build() {
20-
let configs = {
19+
const configs = {
2120
renderer: Object.assign({}, this.rendererDefault, this.config.renderer),
2221
main: Object.assign({}, this.mainDefault, this.config.main)
2322
};
@@ -42,15 +41,16 @@ class WebpackConfig {
4241

4342
settings() {
4443
const json = JSON.parse(fs.readFileSync(bozon.source('package.json')));
45-
let settings = bozon.config(this.env, this.platform);
44+
const settings = bozon.config(this.env, this.platform);
4645
settings.name = json.name;
4746
settings.version = json.version;
4847
return settings;
4948
}
5049

5150
localConfig() {
52-
let configFile = bozon.source('webpack.config.js');
53-
this.config = fs.existsSync(configFile) ? eval(fs.readFileSync(configFile).toString( )) : {};
51+
const configFile = bozon.source('webpack.config.js');
52+
// eslint-disable-next-line no-eval
53+
this.config = fs.existsSync(configFile) ? eval(fs.readFileSync(configFile).toString()) : {};
5454
}
5555
}
5656

src/building/webpack/main.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
const bozon = require("./../../utils/bozon");
1+
const bozon = require('./../../utils/bozon');
22

33
module.exports = (mode, env) => {
44
return {
5-
target: "electron-main",
6-
entry: bozon.sourcePath("main/index.js"),
5+
target: 'electron-main',
6+
entry: bozon.sourcePath('main/index.js'),
77
mode: mode,
88
node: {
99
__dirname: false,
1010
__filename: false
1111
},
1212
output: {
13-
path: bozon.destinationPath("main", env),
14-
filename: "index.js"
13+
path: bozon.destinationPath('main', env),
14+
filename: 'index.js'
1515
},
1616
resolve: {
17-
modules: [bozon.sourcePath("resources")]
17+
modules: [bozon.sourcePath('resources')]
1818
},
1919
plugins: []
2020
};

src/building/webpack/preload.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
const bozon = require("./../../utils/bozon");
1+
const bozon = require('./../../utils/bozon');
22

33
module.exports = (mode, env) => {
44
return {
5-
target: "electron-preload",
6-
entry: bozon.sourcePath("preload/index.js"),
5+
target: 'electron-preload',
6+
entry: bozon.sourcePath('preload/index.js'),
77
mode: mode,
88
node: {
99
__dirname: false,
1010
__filename: false
1111
},
1212
output: {
13-
path: bozon.destinationPath("preload", env),
14-
filename: "index.js"
13+
path: bozon.destinationPath('preload', env),
14+
filename: 'index.js'
1515
},
1616
plugins: []
1717
};

0 commit comments

Comments
 (0)