-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
59 lines (48 loc) · 1.56 KB
/
index.js
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
const path = require('path');
const execa = require('execa');
const readPkgUp = require('read-pkg-up');
const writePkg = require('write-pkg');
const pathExists = require('path-exists');
const {writeFileSync} = require('fs')
const template = `const fs = require('fs')
const posthtml = require('posthtml')
const html = fs.readFileSync('index.html', 'utf-8')
const plugins = []
const options = {}
posthtml(plugins)
.process(html, options)
.then((result) => console.log(result.html))`
const html = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>create-posthtml</title>
</head>
<body>
</body>
</html>`;
const buildScript = build => {
if (build) {
return build;
}
return 'node index.js';
};
module.exports = async (options = {}) => {
const packageResult = readPkgUp.sync({
cwd: options.cwd,
normalize: false
}) || {};
const packageJson = packageResult.package || {};
const packagePath = packageResult.path || path.resolve(options.cwd || '', 'package.json');
const packageCwd = path.dirname(packagePath);
packageJson.scripts = packageJson.scripts || {};
packageJson.scripts.build = buildScript(packageJson.scripts.build);
if (!pathExists.sync(path.join(packageCwd, 'index.js'))) {
writeFileSync(path.resolve(options.cwd || '', 'index.js'), template)
}
if (!pathExists.sync(path.join(packageCwd, 'index.html'))) {
writeFileSync(path.resolve(options.cwd || '', 'index.html'), html)
}
writePkg.sync(packagePath, packageJson);
await execa('npm', ['install', '--save-dev', 'posthtml'], {cwd: packageCwd});
}