index.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. #!/usr/bin/env node
  2. import fs from 'fs-extra';
  3. import path from 'node:path';
  4. import { fileURLToPath } from 'node:url';
  5. import * as p from '@clack/prompts';
  6. import { bold, cyan, grey, red, blue } from 'kleur/colors';
  7. import minimist from 'minimist';
  8. import * as langAll from './lang';
  9. // 获取版本号
  10. // Get version
  11. const { version } = JSON.parse(fs.readFileSync(new URL('../package.json', import.meta.url), 'utf-8'));
  12. // 显示版本号
  13. // Display version number
  14. console.log(`
  15. ${grey(`create-stdf@${version}
  16. `)}`);
  17. const spinner = p.spinner();
  18. p.intro('Welcome to use STDF!');
  19. let lang = langAll.en_US;
  20. // 获取命令行参数
  21. // Get command line parameters
  22. const argv = minimist(process.argv.slice(2));
  23. // 获取项目名称和模板名称和语言
  24. // Get project name and template name
  25. const argvProjectName = argv._[0];
  26. const argvTemplate = argv.template || argv.t;
  27. const argvLanguage = argv.language || argv.l;
  28. // 语言列表
  29. // Language list
  30. const languages = [];
  31. // 循环 langAll 对象,将语言列表中的语言名字替换为对应的语言名字,且按照 sort 排序
  32. // Loop through the langAll object and replace the language name in the language list with the corresponding language name, and sort by sort
  33. for (const key in langAll) {
  34. languages.push({
  35. value: key,
  36. label: langAll[key].name,
  37. sort: langAll[key].sort,
  38. });
  39. }
  40. // 按照 sort 排序
  41. // Sort by sort
  42. languages.sort((a, b) => a.sort - b.sort);
  43. // 如果命令行参数中有语言,且语言列表中有该语言,使用该语言,否则使用英语
  44. // If there is a language in the command line parameters and the language list has the language, use the language, otherwise use English
  45. lang = argvLanguage && languages.find(item => item.value === argvLanguage) ? langAll[argvLanguage] : langAll.en_US;
  46. // 模板列表
  47. // Template list
  48. const templateOptions = [
  49. { value: 'skt', label: 'SvelteKit + Tailwind', template: 'sveltekit-tailwind', finish: true },
  50. { value: 'vt', label: 'Vite + Tailwind', template: 'vite-tailwind', finish: true },
  51. { value: 'vu', label: 'Vite + UnoCSS', template: 'vite-uno', finish: true },
  52. { value: 'sku', label: 'SvelteKit + UnoCSS', template: 'sveltekit-uno', finish: true },
  53. { value: 'vtt', label: 'Vite + Tailwind + TypeScript', template: 'vite-tailwind-typescript', finish: false },
  54. { value: 'vut', label: 'Vite + UnoCSS + TypeScript', template: 'vite-uno-typescript', finish: false },
  55. { value: 'sktt', label: 'SvelteKit + Tailwind + TypeScript', template: 'sveltekit-tailwind-typescript', finish: false },
  56. { value: 'skut', label: 'SvelteKit + UnoCSS + TypeScript', template: 'sveltekit-uno-typescript', finish: false },
  57. ];
  58. // 如果命令行参数中有项目名称,但没有模板名称,直接使用默认模板 skt
  59. // If there is a project name in the command line parameters, but no template name, use the default template skt directly
  60. if (argvProjectName && !argvTemplate) {
  61. createFunc(argvProjectName, templateOptions[0]);
  62. }
  63. // 如果命令行参数中有项目名称和模板名称,直接使用命令行参数中的值
  64. // If there is a project name and template name in the command line parameters, use the value in the command line parameters directly
  65. else if (argvProjectName && argvTemplate) {
  66. const item = templateOptions.find(item => item.value === argvTemplate);
  67. if (!item) {
  68. p.intro(red(lang.pectn));
  69. } else if (!item.finish) {
  70. p.intro(red(item.label + ' ' + lang.hnay));
  71. } else {
  72. createFunc(argvProjectName, item);
  73. }
  74. } else {
  75. (async () => {
  76. // 选择一种语言
  77. // Select a language
  78. const languageType = await p.select({
  79. message: bold('Please select your preferred language'),
  80. options: languages,
  81. });
  82. if (p.isCancel(languageType)) {
  83. p.cancel(red('⛔ ') + lang.oc);
  84. process.exit(0);
  85. }
  86. lang = langAll[languageType];
  87. // 选择一个模板
  88. // Select a template
  89. let template = await p.select({
  90. message: bold(lang.psat),
  91. options: templateOptions,
  92. });
  93. if (p.isCancel(template)) {
  94. p.cancel(red('⛔ ') + lang.oc);
  95. process.exit(0);
  96. }
  97. // 直到选择的 template 所在项 的 finish 为 true 为止,否则一直重新选择
  98. // Until the finish of the selected template is true, otherwise keep reselecting
  99. while (!templateOptions.find(item => item.value === template)?.finish) {
  100. if (p.isCancel(template)) {
  101. p.cancel(red('⛔ ') + lang.oc);
  102. process.exit(0);
  103. }
  104. p.intro(red(templateOptions.find(item => item.value === template).label + ' ' + lang.hnay + ' ' + lang.pca));
  105. template = await p.select({
  106. message: bold(lang.psat),
  107. options: templateOptions,
  108. });
  109. }
  110. // 输入项目名称
  111. // Enter the project name
  112. const projectName = await p.text({
  113. message: bold(lang.pn),
  114. placeholder: 'stdf-project',
  115. validate: value => {
  116. if (!value) {
  117. // 判断是否为空,提示 “项目名称不能为空”
  118. // Determine whether it is empty, prompt "Project name cannot be empty"
  119. return lang.pncbne;
  120. }
  121. if (fs.existsSync(value)) {
  122. // 判断是否已存在,提示 “项目名称已存在”
  123. // Determine whether it already exists, prompt "Project name already exists"
  124. return lang.pane;
  125. }
  126. },
  127. });
  128. if (p.isCancel(projectName)) {
  129. p.cancel(red('⛔ ') + lang.oc);
  130. process.exit(0);
  131. }
  132. // 根据 template 的值,复制对应目录下的所有文件到当前目录
  133. // According to the value of template, copy all files under the corresponding directory to the current directory
  134. templateOptions.forEach(async item => {
  135. if (item.value === template) {
  136. createFunc(projectName, item);
  137. }
  138. });
  139. })();
  140. }
  141. function createFunc(projectName, item) {
  142. // 如果 projectName 是数字,转为字符串
  143. // If projectName is a number, convert it to a string
  144. if (typeof projectName === 'number') {
  145. projectName = projectName.toString();
  146. }
  147. // 项目目录
  148. // Project directory
  149. const projectDir = path.join(path.resolve(), projectName);
  150. spinner.start('🚀 ' + lang.cfsing);
  151. fs.mkdirSync(projectDir);
  152. // 获取模板目录的绝对路径,考虑到 Windows 系统的兼容性, 使用 path.join
  153. // Get the absolute path of the template directory, considering the compatibility of the Windows system, use path.join
  154. const templatePath = path.resolve(fileURLToPath(import.meta.url), '../..', `templates/${item.template}`);
  155. // 将 templatePath 目录下的所有文件复制到 projectDir 目录下
  156. // Copy all files under the templatePath directory to the projectDir directory\
  157. fs.copy(templatePath, projectDir)
  158. .then(() => {
  159. spinner.stop();
  160. p.outro(`${projectName} - ${lang.pcsucc} 🎉`);
  161. // 读取 package.json 文件
  162. // Read the package.json file
  163. const packageJson = JSON.parse(fs.readFileSync(`${projectDir}/package.json`, 'utf-8'));
  164. // 将项目内的 package.json 中的 name 属性修改为 projectName
  165. // Modify the name attribute in package.json in the project to projectName
  166. packageJson.name = projectName;
  167. // 将修改后的 packageJson 写入到项目内的 package.json 文件中
  168. // Write the modified packageJson to the package.json file in the project
  169. fs.writeFileSync(`${projectDir}/package.json`, JSON.stringify(packageJson, null, 4), 'utf-8');
  170. // 根据 item.value 的值,判断是否使用了 @sveltejs/kit
  171. // According to the value of item.value, determine whether @sveltejs/kit is used
  172. const isHasKit = item.value === 'skt' || item.value === 'sku' || item.value === 'sktt' || item.value === 'skut';
  173. // 根据 item.value 的值,判断使用 Tailwind 还是 UnoCSS
  174. // According to the value of item.value, determine whether to use Tailwind or UnoCSS
  175. const isHasUno = item.value === 'vu' || item.value === 'sku' || item.value === 'vut' || item.value === 'skut';
  176. // 获得依赖的版本号
  177. // get the version number of the dependency
  178. const versions = {
  179. vite: packageJson.devDependencies.vite.replace('^', ''),
  180. svelte: packageJson.devDependencies.svelte.replace('^', ''),
  181. stdf: packageJson.devDependencies.stdf.replace('^', ''),
  182. };
  183. if (isHasKit) {
  184. versions['@sveltejs/kit'] = packageJson.devDependencies['@sveltejs/kit'].replace('^', '');
  185. }
  186. if (isHasUno) {
  187. versions['unocss'] = packageJson.devDependencies.unocss.replace('^', '');
  188. } else {
  189. versions['tailwindcss'] = packageJson.devDependencies.tailwindcss.replace('^', '');
  190. }
  191. // 将 versions 的键值拼接为 bold('Vite:') cyan(versions.vite) bold('Svelte:') cyan(versions.svelte) 的形式
  192. // Splice the key value of versions into the form of bold('Vite:') cyan(versions.vite) bold('Svelte:') cyan(versions.svelte)
  193. let versionsString = '';
  194. for (const key in versions) {
  195. versionsString += bold(key) + ': ' + cyan(versions[key]) + ' ';
  196. }
  197. // 显示版本号
  198. // Display version number
  199. console.log(`📦 ${versionsString}
  200. `);
  201. // 显示提示信息
  202. // Display prompt information
  203. console.log(
  204. `👉 ${bold(lang.tgs)}
  205. ${blue(`cd ${projectName}`)}
  206. ${blue('bun i / pnpm i / npm i / yarn')}
  207. ${blue('bun dev / npm run dev')}
  208. `,
  209. );
  210. // 显示配置主题色
  211. // Display configuration theme color
  212. console.log(
  213. `🎨 ${grey(isHasUno ? lang.pcyt_vu : lang.pcyt_vt)}
  214. `,
  215. );
  216. })
  217. .catch(err => {
  218. spinner.stop();
  219. console.error(red(lang.cferror + '--' + err));
  220. });
  221. }