-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.js
executable file
·88 lines (72 loc) · 2.63 KB
/
generate.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env node
import { Command } from "commander";
import fs from "fs-extra";
import ignore from "ignore";
import inquirer from "inquirer";
import { fileURLToPath } from "node:url";
import path from "path";
import { generateIcons } from "./logo.js";
const program = new Command();
program
.command("logo")
.description("Generate logo icons")
.argument("<svg_path>", "path to svg icon")
.action((path) => {
generateIcons(path);
});
program.action(() => generateProject().catch(console.error));
program.parse(process.argv);
async function generateProject() {
const answers = await inquirer.prompt([
{ name: "name", message: "Project name:", default: "my-crx-app" },
{ name: "version", message: "Version:", default: "0.0.0" },
{ name: "description", message: "Description:", default: "" },
{ name: "author", message: "Author:", default: "" },
]);
const templateDir = path.resolve(
fileURLToPath(import.meta.url),
"..",
"template"
);
const outputDir = path.join(process.cwd(), answers.name);
// await fs.copy(templateDir, outputDir);
// console.log(`Copied template to ${outputDir}`);
// Load .gitignore rules
const gitignorePath = path.join(templateDir, ".gitignore");
let ig = ignore();
if (fs.existsSync(gitignorePath)) {
const gitignoreContent = fs.readFileSync(gitignorePath, "utf8");
ig = ignore().add(gitignoreContent);
}
// Copy template with .gitignore filtering
await fs.copy(templateDir, outputDir, {
filter: (src) => {
const relativePath = path.relative(templateDir, src);
return relativePath === "" || !ig.ignores(relativePath);
},
});
// Update package.json
const packageJsonPath = path.join(outputDir, "package.json");
let packageJson = await fs.readFile(packageJsonPath, "utf-8");
const pkg = JSON.parse(packageJson, "utf-8");
pkg.name = answers.name;
pkg.displayName = answers.name;
pkg.version = answers.version;
pkg.description = answers.description;
pkg.author = answers.author;
await fs.writeFile(packageJsonPath, JSON.stringify(pkg, null, 2));
// Update README.md
const readmePath = path.join(outputDir, "README.md");
let readmeContent = await fs.readFile(readmePath, "utf8");
readmeContent = readmeContent.replace(/my-crx-app/g, answers.name);
await fs.writeFile(readmePath, readmeContent);
console.log(`
Suggest you next step:
1. cd ${path.relative(process.cwd(), outputDir)}
2. Run yarn install
3. Open chrome://extensions/ in your browser
4. Check the box for Developer mode in the top right.
5. Click the Load unpacked extension button.
6. Select the build/ directory that was created.
`);
}