cmyr-template-cli 1.29.2 → 1.30.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -68,7 +68,8 @@ ct create
68
68
  "PATREON_USERNAME": "",
69
69
  "WEIBO_USERNAME": "",
70
70
  "TWITTER_USERNAME": "",
71
- "NPM_USERNAME": ""
71
+ "NPM_USERNAME": "",
72
+ "DOCKER_USERNAME": ""
72
73
  }
73
74
  ```
74
75
 
@@ -90,6 +91,8 @@ TWITTER_USERNAME:Twitter 用户名,可空,默认值为空
90
91
 
91
92
  NPM_USERNAME:Npm 用户名,可空,默认会使用 `GITHUB_USERNAME` 的用户名
92
93
 
94
+ DOCKER_USERNAME:Docker Hub 用户名,可空,默认会使用**小写的** `GITHUB_USERNAME` 的用户名
95
+
93
96
  **如果不使用自动初始化远程仓库功能,可以跳过该配置**
94
97
 
95
98
  ## 开发
package/dist/plopfile.js CHANGED
@@ -14,6 +14,8 @@ var core = require('@lint-md/core');
14
14
  var JSON5 = require('json5');
15
15
  var os = require('os');
16
16
  var yaml = require('yaml');
17
+ var acorn = require('acorn');
18
+ var walk = require('acorn-walk');
17
19
 
18
20
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
19
21
 
@@ -45,6 +47,8 @@ var ejs__default = /*#__PURE__*/_interopDefaultLegacy(ejs);
45
47
  var JSON5__default = /*#__PURE__*/_interopDefaultLegacy(JSON5);
46
48
  var os__default = /*#__PURE__*/_interopDefaultLegacy(os);
47
49
  var yaml__default = /*#__PURE__*/_interopDefaultLegacy(yaml);
50
+ var acorn__default = /*#__PURE__*/_interopDefaultLegacy(acorn);
51
+ var walk__default = /*#__PURE__*/_interopDefaultLegacy(walk);
48
52
 
49
53
  process.env;
50
54
  const PACKAGE_MANAGER = 'pnpm';
@@ -495,6 +499,7 @@ async function init(projectPath, answers) {
495
499
  }
496
500
  await sortProjectJson(projectPath);
497
501
  await initYarn(projectPath, answers);
502
+ await jsFileExtRename(projectPath);
498
503
  await asyncExec('git add .', {
499
504
  cwd: projectPath,
500
505
  });
@@ -900,7 +905,8 @@ async function getProjectInfo(projectPath, answers) {
900
905
  const twitterUsername = config === null || config === void 0 ? void 0 : config.TWITTER_USERNAME;
901
906
  const afdianUsername = config === null || config === void 0 ? void 0 : config.AFDIAN_USERNAME;
902
907
  const patreonUsername = config === null || config === void 0 ? void 0 : config.PATREON_USERNAME;
903
- const npmUsername = (config === null || config === void 0 ? void 0 : config.NPM_USERNAME) || author;
908
+ const npmUsername = (config === null || config === void 0 ? void 0 : config.NPM_USERNAME) || githubUsername;
909
+ const dockerUsername = (config === null || config === void 0 ? void 0 : config.DOCKER_USERNAME) || (githubUsername === null || githubUsername === void 0 ? void 0 : githubUsername.toLowerCase());
904
910
  const repositoryUrl = `https://github.com/${githubUsername}/${projectName}`;
905
911
  const gitUrl = `git+${repositoryUrl}.git`;
906
912
  const issuesUrl = `${repositoryUrl}/issues`;
@@ -966,6 +972,7 @@ async function getProjectInfo(projectPath, answers) {
966
972
  weiboUsername,
967
973
  twitterUsername,
968
974
  npmUsername,
975
+ dockerUsername,
969
976
  templateMeta,
970
977
  mainFile,
971
978
  };
@@ -1043,14 +1050,7 @@ async function initLicense(projectPath, projectInfos) {
1043
1050
  async function initConfig(projectPath) {
1044
1051
  try {
1045
1052
  await removeFiles(projectPath, ['commitlint.config.cjs', 'commitlint.config.js']);
1046
- const pkg = await getProjectJson(projectPath);
1047
- const files = ['.editorconfig'];
1048
- if (pkg.type === 'module') {
1049
- files.push('commitlint.config.cjs');
1050
- }
1051
- else {
1052
- files.push('commitlint.config.js');
1053
- }
1053
+ const files = ['.editorconfig', 'commitlint.config.js'];
1054
1054
  await copyFilesFromTemplates(projectPath, files);
1055
1055
  }
1056
1056
  catch (error) {
@@ -1088,12 +1088,7 @@ async function initSemanticRelease(projectPath) {
1088
1088
  const pkg = await getProjectJson(projectPath);
1089
1089
  const files = ['.releaserc.js', '.releaserc.cjs'];
1090
1090
  await removeFiles(projectPath, files);
1091
- if (pkg.type === 'module') {
1092
- await copyFilesFromTemplates(projectPath, ['.releaserc.cjs']);
1093
- }
1094
- else {
1095
- await copyFilesFromTemplates(projectPath, ['.releaserc.js']);
1096
- }
1091
+ await copyFilesFromTemplates(projectPath, ['.releaserc.js']);
1097
1092
  const devDependencies = {
1098
1093
  '@semantic-release/changelog': '^6.0.3',
1099
1094
  '@semantic-release/git': '^10.0.1',
@@ -1224,18 +1219,7 @@ async function initEslint(projectPath, answers) {
1224
1219
  }`;
1225
1220
  const cjsPath = path__default["default"].join(projectPath, '.eslintrc.cjs');
1226
1221
  const jsPath = path__default["default"].join(projectPath, '.eslintrc.js');
1227
- if (pkg.type === 'module') {
1228
- await removeFiles(projectPath, ['.eslintrc.js']);
1229
- if (!await fs__default["default"].pathExists(cjsPath)) {
1230
- if (await fs__default["default"].pathExists(jsPath)) {
1231
- await fs__default["default"].rename(jsPath, cjsPath);
1232
- }
1233
- else {
1234
- await fs__default["default"].writeFile(cjsPath, eslintrc);
1235
- }
1236
- }
1237
- }
1238
- else if (!await fs__default["default"].pathExists(jsPath)) {
1222
+ if (!await fs__default["default"].pathExists(cjsPath) && !await fs__default["default"].pathExists(jsPath)) {
1239
1223
  await fs__default["default"].writeFile(jsPath, eslintrc);
1240
1224
  }
1241
1225
  loading.succeed('eslint 初始化成功!');
@@ -1263,13 +1247,8 @@ async function initStylelint(projectPath) {
1263
1247
  });
1264
1248
  return;
1265
1249
  }
1266
- const files = ['.stylelintignore'];
1267
- if (pkg.type === 'module') {
1268
- files.push('.stylelintrc.cjs');
1269
- }
1270
- else {
1271
- files.push('.stylelintrc.js');
1272
- }
1250
+ const files = ['.stylelintignore', '.stylelintrc.js'];
1251
+ await removeFiles(projectPath, ['.stylelintrc.js', '.stylelintrc.cjs']);
1273
1252
  await copyFilesFromTemplates(projectPath, files);
1274
1253
  const devDependencies = {
1275
1254
  'postcss-html': '^1.5.0',
@@ -1415,6 +1394,96 @@ async function initJest(projectPath) {
1415
1394
  loading.fail('Jest 初始化失败!');
1416
1395
  }
1417
1396
  }
1397
+ async function jsFileExtRename(projectPath) {
1398
+ const loading = ora__default["default"]('正在重命名 js 后缀名 ……').start();
1399
+ try {
1400
+ const jsFiles = (await fs__default["default"].readdir(projectPath)).filter((file) => /\.js$/.test(file)).map((file) => path__default["default"].join(projectPath, file));
1401
+ const pkg = await getProjectJson(projectPath);
1402
+ if (pkg.type === 'module') {
1403
+ for await (const filepath of jsFiles) {
1404
+ const fileContent = await fs__default["default"].readFile(filepath, 'utf-8');
1405
+ const moduleType = getJsModuleType(fileContent);
1406
+ console.log(`正在判断文件:${filepath} 的模块类型`);
1407
+ if (moduleType === 'CommonJS') {
1408
+ const dirpath = path__default["default"].dirname(filepath);
1409
+ const extname = path__default["default"].extname(filepath);
1410
+ const basename = `${path__default["default"].basename(filepath, extname)}.cjs`;
1411
+ const newPath = path__default["default"].join(dirpath, basename);
1412
+ await fs__default["default"].rename(filepath, newPath);
1413
+ }
1414
+ }
1415
+ }
1416
+ else if (pkg.type === 'commonjs') {
1417
+ for await (const filepath of jsFiles) {
1418
+ const fileContent = await fs__default["default"].readFile(filepath, 'utf-8');
1419
+ const moduleType = getJsModuleType(fileContent);
1420
+ console.log(`正在判断文件:${filepath} 的模块类型`);
1421
+ if (moduleType === 'EsModule') {
1422
+ const dirpath = path__default["default"].dirname(filepath);
1423
+ const extname = path__default["default"].extname(filepath);
1424
+ const basename = `${path__default["default"].basename(filepath, extname)}.mjs`;
1425
+ const newPath = path__default["default"].join(dirpath, basename);
1426
+ await fs__default["default"].rename(filepath, newPath);
1427
+ }
1428
+ }
1429
+ }
1430
+ loading.succeed('重命名 js 后缀名成功!');
1431
+ }
1432
+ catch (error) {
1433
+ loading.fail('重命名 js 后缀名失败!');
1434
+ console.error(error);
1435
+ }
1436
+ }
1437
+ function getJsModuleType(fileContent) {
1438
+ try {
1439
+ const ast = acorn__default["default"].parse(fileContent, {
1440
+ sourceType: 'module',
1441
+ ecmaVersion: 'latest',
1442
+ });
1443
+ let isCommonJS = false;
1444
+ let isESModule = false;
1445
+ walk__default["default"].simple(ast, {
1446
+ AssignmentExpression(node) {
1447
+ var _a, _b, _c, _d;
1448
+ if (((_b = (_a = node.left) === null || _a === void 0 ? void 0 : _a.object) === null || _b === void 0 ? void 0 : _b.name) === 'module' && ((_d = (_c = node.left) === null || _c === void 0 ? void 0 : _c.property) === null || _d === void 0 ? void 0 : _d.name) === 'exports') {
1449
+ isCommonJS = true;
1450
+ }
1451
+ },
1452
+ CallExpression(node) {
1453
+ var _a;
1454
+ if (((_a = node.callee) === null || _a === void 0 ? void 0 : _a.name) === 'require') {
1455
+ isCommonJS = true;
1456
+ }
1457
+ },
1458
+ ImportDeclaration(node) {
1459
+ isESModule = true;
1460
+ },
1461
+ ExportAllDeclaration(node) {
1462
+ isESModule = true;
1463
+ },
1464
+ ExportDefaultDeclaration(node) {
1465
+ isESModule = true;
1466
+ },
1467
+ ExportNamedDeclaration(node) {
1468
+ isESModule = true;
1469
+ },
1470
+ ExportSpecifier(node) {
1471
+ isESModule = true;
1472
+ },
1473
+ });
1474
+ if (isESModule) {
1475
+ return 'EsModule';
1476
+ }
1477
+ if (isCommonJS) {
1478
+ return 'CommonJS';
1479
+ }
1480
+ return 'Unknown';
1481
+ }
1482
+ catch (error) {
1483
+ console.error(error);
1484
+ return 'Unknown';
1485
+ }
1486
+ }
1418
1487
  async function sortProjectJson(projectPath) {
1419
1488
  try {
1420
1489
  const pkg = await getProjectJson(projectPath);
@@ -1822,7 +1891,7 @@ module.exports = function (plop) {
1822
1891
  type: 'confirm',
1823
1892
  name: 'isRemoveYarn',
1824
1893
  message: '是否移除 yarn ?',
1825
- default: false,
1894
+ default: true,
1826
1895
  when(answers) {
1827
1896
  return answers.isOpenSource;
1828
1897
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cmyr-template-cli",
3
- "version": "1.29.2",
3
+ "version": "1.30.1",
4
4
  "description": "草梅友仁自制的项目模板创建器",
5
5
  "author": "CaoMeiYouRen",
6
6
  "license": "MIT",
@@ -46,8 +46,8 @@
46
46
  "@types/lodash": "^4.14.165",
47
47
  "@types/node": "^20.0.0",
48
48
  "@types/promise.any": "^2.0.0",
49
- "@typescript-eslint/eslint-plugin": "7.15.0",
50
- "@typescript-eslint/parser": "7.15.0",
49
+ "@typescript-eslint/eslint-plugin": "7.16.1",
50
+ "@typescript-eslint/parser": "7.16.1",
51
51
  "commitizen": "^4.2.2",
52
52
  "conventional-changelog-cli": "^5.0.0",
53
53
  "conventional-changelog-cmyr-config": "^2.1.1",
@@ -58,7 +58,7 @@
58
58
  "eslint-config-cmyr": "^1.1.27",
59
59
  "husky": "^9.0.5",
60
60
  "lint-staged": "^15.0.1",
61
- "rimraf": "^5.0.0",
61
+ "rimraf": "^6.0.0",
62
62
  "rollup": "^2.79.0",
63
63
  "rollup-plugin-terser": "^7.0.2",
64
64
  "semantic-release": "21.1.2",
@@ -68,6 +68,8 @@
68
68
  },
69
69
  "dependencies": {
70
70
  "@lint-md/core": "^2.0.0",
71
+ "acorn": "^8.12.1",
72
+ "acorn-walk": "^8.3.3",
71
73
  "axios": "^1.0.0",
72
74
  "colors": "^1.4.0",
73
75
  "commander": "^12.0.0",
@@ -13,7 +13,10 @@ jobs:
13
13
  runs-on: ubuntu-latest
14
14
  timeout-minutes: 10
15
15
  permissions:
16
+ packages: write
16
17
  contents: write
18
+ issues: write
19
+ pull-requests: write
17
20
  steps:
18
21
  - uses: actions/checkout@v4
19
22
  with:
@@ -4,10 +4,18 @@
4
4
  <a href="https://www.npmjs.com/package/<%= packageName %>" target="_blank">
5
5
  <img alt="Version" src="https://img.shields.io/npm/v/<%= packageName %>.svg">
6
6
  </a>
7
+ <a href="https://www.npmjs.com/package/<%= packageName %>" target="_blank">
8
+ <img alt="npm downloads" src="https://img.shields.io/npm/dt/<%= packageName %>?label=npm%20downloads&color=yellow">
9
+ </a>
7
10
  <% } -%>
8
11
  <% if (projectVersion && isJSProject) { -%>
9
12
  <img alt="Version" src="https://img.shields.io/github/package-json/v/<%= authorGithubUsername %>/<%= packageName %>.svg" />
10
13
  <% } -%>
14
+ <% if (isOpenSource && isInitDocker) { -%>
15
+ <a href="https://hub.docker.com/r/<%= dockerUsername %>/<%= projectName %>" target="_blank">
16
+ <img alt="Docker Pulls" src="https://img.shields.io/docker/pulls/<%= dockerUsername %>/<%= projectName %>">
17
+ </a>
18
+ <% } -%>
11
19
  <% if (isGithubRepos && isInitSemanticRelease) { -%>
12
20
  <a href="<%= repositoryUrl %>/actions?query=workflow%3ARelease" target="_blank">
13
21
  <img alt="GitHub Workflow Status" src="https://img.shields.io/github/actions/workflow/status/<%= authorGithubUsername %>/<%= projectName %>/release.yml?branch=master">
@@ -17,5 +17,6 @@ module.exports = {
17
17
  'subject-full-stop': [0, 'never'],
18
18
  'subject-case': [0, 'never'],
19
19
  'body-max-line-length': [0, 'never'],
20
+ 'footer-max-line-length': [0, 'never'],
20
21
  },
21
22
  }
@@ -17,5 +17,6 @@ module.exports = {
17
17
  'subject-full-stop': [0, 'never'],
18
18
  'subject-case': [0, 'never'],
19
19
  'body-max-line-length': [0, 'never'],
20
+ 'footer-max-line-length': [0, 'never'],
20
21
  },
21
22
  }