Skip to content

Commit b56b832

Browse files
committed
feat: remove jquery
1 parent e2a7103 commit b56b832

File tree

3 files changed

+51
-12
lines changed

3 files changed

+51
-12
lines changed

cli.js

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
const yargs_parser = require("yargs-parser");
44
const path = require("path");
55
const chalk = require("chalk");
6+
const prompts = require("prompts");
7+
const semver = require("semver");
68
const ora = require("ora");
79
const { extract } = require("pacote");
810
const glob = require("fast-glob");
@@ -11,6 +13,7 @@ const os = require("os");
1113
const packageName = "html5-boilerplate";
1214
const tempDir = os.tmpdir() + `/${packageName}-staging`;
1315
const elapsed = require("elapsed-time-logger");
16+
let spinner;
1417

1518
module.exports = async () => {
1619
const argv = yargs_parser(process.argv.slice(2), {
@@ -19,7 +22,7 @@ module.exports = async () => {
1922
const timer = elapsed.start();
2023
const version = argv["release"] || "latest";
2124
const targetDir = path.resolve(argv["_"][0] || "./");
22-
const spinner = ora(
25+
spinner = ora(
2326
`Downloading ${packageName} version '${version}' to ${targetDir}`
2427
).start();
2528
await fs.ensureDir(tempDir);
@@ -29,9 +32,13 @@ module.exports = async () => {
2932
tempDir,
3033
{}
3134
);
32-
spinner.text = `${nameWithVersion} copied to ${targetDir} in ${timer.get()}. Have fun!`;
35+
await fs.copy(tempDir + "/dist", targetDir);
36+
await onLoad(targetDir, version);
37+
spinner.succeed(
38+
`${nameWithVersion} copied to ${targetDir} in ${timer.get()}. Have fun!`
39+
);
40+
return;
3341
} catch (err) {
34-
await fs.remove(tempDir);
3542
if (err.code === "ETARGET") {
3643
const msg = chalk.red(
3744
`version '${err.wanted}' not found in npm registry\navailable versions:\n`
@@ -41,16 +48,49 @@ module.exports = async () => {
4148
}
4249
spinner.fail("Unexpected error");
4350
throw new Error(err);
51+
} finally {
52+
await fs.remove(tempDir);
4453
}
45-
await fs.copy(tempDir + "/dist", targetDir);
46-
await fs.remove(tempDir);
54+
};
55+
56+
const onLoad = async (targetDir, version) => {
4757
// see https://github.com/mrmlnc/fast-glob#how-to-write-patterns-on-windows
4858
const npmIgnoreFiles = await glob(
4959
`${targetDir.replace(/\\/g, "/")}/**/.npmignore`
5060
);
5161
for (const npmIgnore of npmIgnoreFiles) {
5262
await fs.rename(npmIgnore, npmIgnore.replace(/\.npmignore$/, ".gitignore"));
5363
}
54-
spinner.succeed();
55-
return;
64+
65+
spinner.stop();
66+
67+
const questions = [
68+
{
69+
type: "confirm",
70+
name: "jquery",
71+
message: "Remove jQuery?",
72+
initial: true,
73+
},
74+
// {
75+
// type: 'confirm',
76+
// name: 'docs',
77+
// message: 'Include docs?',
78+
// }
79+
];
80+
81+
const { jquery } = await prompts(questions);
82+
83+
if (jquery) {
84+
try {
85+
const indexFile = targetDir + "/index.html";
86+
const sourceHTML = await fs.readFile(indexFile, "utf-8");
87+
const resultHTML = sourceHTML.replace(
88+
/(<script>window\.jQuery.*<\/script>|<script src=".*jquery.*<\/script>)/gi,
89+
""
90+
);
91+
await fs.writeFile(indexFile, resultHTML);
92+
} catch (err) {
93+
throw new Error(err);
94+
}
95+
}
5696
};

package-lock.json

Lines changed: 2 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
"fs-extra": "^9.0.0",
3737
"ora": "^4.0.4",
3838
"pacote": "^11.1.10",
39+
"prompts": "^2.3.2",
40+
"semver": "^7.3.2",
3941
"yargs-parser": "^18.1.3"
4042
},
4143
"devDependencies": {

0 commit comments

Comments
 (0)