-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathnew_template.js
115 lines (95 loc) · 3.54 KB
/
new_template.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import chalk from 'chalk';
import ChildProcess, { execSync } from 'child_process';
import fs from 'fs';
import path from 'path';
const createTemplate = (templateName) => {
console.log(chalk.cyan(`\nCreating new template '${chalk.bold(templateName)}'...`));
const packagesDir = './packages';
const baseTemplatePath = path.join(packagesDir, 'example');
const newTemplatePath = path.join(packagesDir, templateName);
// list all templates (those are the folders in packages/)
let templates = ChildProcess.execSync(`ls ./packages`).toString().split('\n');
// remove the last element of the array, which is an empty string
templates.pop();
if (templates.includes(`'${templateName}'`) || fs.existsSync(newTemplatePath)) {
console.log(chalk.red(`× Template '${chalk.bold(templateName)}' already exists.`));
return;
}
// create template folder
if (!fs.existsSync(newTemplatePath)) {
fs.mkdirSync(newTemplatePath);
}
// create pages folder
if (!fs.existsSync(path.join(newTemplatePath, 'pages'))) {
// create pages folder
fs.mkdirSync(path.join(newTemplatePath, 'pages'));
// create api folder
fs.mkdirSync(path.join(newTemplatePath, 'pages/api'));
}
// Copy files from base template
const filesToCopy = [
'package.json',
'pages/index.tsx',
'pages/_app.tsx',
'pages/_document.tsx',
'pages/404.tsx',
'pages/500.tsx',
'pages/api/hello.ts',
'tsconfig.json',
'next.config.js',
'next-env.d.ts'
];
try {
filesToCopy.forEach((file) => {
const srcPath = path.join(baseTemplatePath, file);
const destPath = path.join(newTemplatePath, file);
fs.copyFileSync(srcPath, destPath);
});
// Update build.js
const buildJsPaths = ['./scripts/build.js', './scripts/share_files.js'];
buildJsPaths.forEach((buildJsPath) => {
let buildJsContent = fs.readFileSync(buildJsPath, 'utf8');
buildJsContent = buildJsContent.replace(
/const templates = \[([^\]]+)\];/,
`const templates = [$1, '${templateName}'];`
);
fs.writeFileSync(buildJsPath, buildJsContent);
});
// Share common files with new template
execSync('npm run share-files', { stdio: 'inherit' });
// Update package.json name property by replacing example with templateName
const packageJsonPath = path.join(newTemplatePath, 'package.json');
let packageJsonContent = fs.readFileSync(packageJsonPath, 'utf8');
// update package name
packageJsonContent = packageJsonContent.replace(
/"name": "create-nextjs-dapp-example"/,
`"name": "create-nextjs-dapp-${templateName}"`
);
// update package version to 1.0.0, without knowing the previous version
packageJsonContent = packageJsonContent.replace(/"version": "[^"]+"/, `"version": "1.0.0"`);
fs.writeFileSync(packageJsonPath, packageJsonContent);
// Instructions for further steps
console.log(
chalk.green(`\n✔ Template '${chalk.bold(templateName)}' created successfully.`) +
chalk.cyan(
`\n\nNavigate to your template folder with ${chalk.underline(
`cd ./packages/${templateName}`
)} and start development with 'npm run dev'.\n`
)
);
} catch (err) {
console.log(chalk.red(`× Error creating template '${chalk.bold(templateName)}'.`));
console.log(chalk.yellow(err));
// clean up template folder
fs.rm(newTemplatePath, { recursive: true }, () => {});
}
};
// Get the template name from the command line arguments
const templateName = process.argv[2];
if (!templateName) {
console.log(chalk.red('Please provide a template name.'));
console.log(chalk.yellow('Example: npm run new-template my-template'));
process.exit(1);
}
// init script
createTemplate(templateName);