cmyr-template-cli 1.29.2 → 1.30.0

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/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
  });
@@ -1043,14 +1048,7 @@ async function initLicense(projectPath, projectInfos) {
1043
1048
  async function initConfig(projectPath) {
1044
1049
  try {
1045
1050
  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
- }
1051
+ const files = ['.editorconfig', 'commitlint.config.js'];
1054
1052
  await copyFilesFromTemplates(projectPath, files);
1055
1053
  }
1056
1054
  catch (error) {
@@ -1088,12 +1086,7 @@ async function initSemanticRelease(projectPath) {
1088
1086
  const pkg = await getProjectJson(projectPath);
1089
1087
  const files = ['.releaserc.js', '.releaserc.cjs'];
1090
1088
  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
- }
1089
+ await copyFilesFromTemplates(projectPath, ['.releaserc.js']);
1097
1090
  const devDependencies = {
1098
1091
  '@semantic-release/changelog': '^6.0.3',
1099
1092
  '@semantic-release/git': '^10.0.1',
@@ -1224,18 +1217,7 @@ async function initEslint(projectPath, answers) {
1224
1217
  }`;
1225
1218
  const cjsPath = path__default["default"].join(projectPath, '.eslintrc.cjs');
1226
1219
  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)) {
1220
+ if (!await fs__default["default"].pathExists(cjsPath) && !await fs__default["default"].pathExists(jsPath)) {
1239
1221
  await fs__default["default"].writeFile(jsPath, eslintrc);
1240
1222
  }
1241
1223
  loading.succeed('eslint 初始化成功!');
@@ -1263,13 +1245,8 @@ async function initStylelint(projectPath) {
1263
1245
  });
1264
1246
  return;
1265
1247
  }
1266
- const files = ['.stylelintignore'];
1267
- if (pkg.type === 'module') {
1268
- files.push('.stylelintrc.cjs');
1269
- }
1270
- else {
1271
- files.push('.stylelintrc.js');
1272
- }
1248
+ const files = ['.stylelintignore', '.stylelintrc.js'];
1249
+ await removeFiles(projectPath, ['.stylelintrc.js', '.stylelintrc.cjs']);
1273
1250
  await copyFilesFromTemplates(projectPath, files);
1274
1251
  const devDependencies = {
1275
1252
  'postcss-html': '^1.5.0',
@@ -1415,6 +1392,96 @@ async function initJest(projectPath) {
1415
1392
  loading.fail('Jest 初始化失败!');
1416
1393
  }
1417
1394
  }
1395
+ async function jsFileExtRename(projectPath) {
1396
+ const loading = ora__default["default"]('正在重命名 js 后缀名 ……').start();
1397
+ try {
1398
+ const jsFiles = (await fs__default["default"].readdir(projectPath)).filter((file) => /\.js$/.test(file)).map((file) => path__default["default"].join(projectPath, file));
1399
+ const pkg = await getProjectJson(projectPath);
1400
+ if (pkg.type === 'module') {
1401
+ for await (const filepath of jsFiles) {
1402
+ const fileContent = await fs__default["default"].readFile(filepath, 'utf-8');
1403
+ const moduleType = getJsModuleType(fileContent);
1404
+ console.log(`正在判断文件:${filepath} 的模块类型`);
1405
+ if (moduleType === 'CommonJS') {
1406
+ const dirpath = path__default["default"].dirname(filepath);
1407
+ const extname = path__default["default"].extname(filepath);
1408
+ const basename = `${path__default["default"].basename(filepath, extname)}.cjs`;
1409
+ const newPath = path__default["default"].join(dirpath, basename);
1410
+ await fs__default["default"].rename(filepath, newPath);
1411
+ }
1412
+ }
1413
+ }
1414
+ else if (pkg.type === 'commonjs') {
1415
+ for await (const filepath of jsFiles) {
1416
+ const fileContent = await fs__default["default"].readFile(filepath, 'utf-8');
1417
+ const moduleType = getJsModuleType(fileContent);
1418
+ console.log(`正在判断文件:${filepath} 的模块类型`);
1419
+ if (moduleType === 'EsModule') {
1420
+ const dirpath = path__default["default"].dirname(filepath);
1421
+ const extname = path__default["default"].extname(filepath);
1422
+ const basename = `${path__default["default"].basename(filepath, extname)}.mjs`;
1423
+ const newPath = path__default["default"].join(dirpath, basename);
1424
+ await fs__default["default"].rename(filepath, newPath);
1425
+ }
1426
+ }
1427
+ }
1428
+ loading.succeed('重命名 js 后缀名成功!');
1429
+ }
1430
+ catch (error) {
1431
+ loading.fail('重命名 js 后缀名失败!');
1432
+ console.error(error);
1433
+ }
1434
+ }
1435
+ function getJsModuleType(fileContent) {
1436
+ try {
1437
+ const ast = acorn__default["default"].parse(fileContent, {
1438
+ sourceType: 'module',
1439
+ ecmaVersion: 'latest',
1440
+ });
1441
+ let isCommonJS = false;
1442
+ let isESModule = false;
1443
+ walk__default["default"].simple(ast, {
1444
+ AssignmentExpression(node) {
1445
+ var _a, _b, _c, _d;
1446
+ 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') {
1447
+ isCommonJS = true;
1448
+ }
1449
+ },
1450
+ CallExpression(node) {
1451
+ var _a;
1452
+ if (((_a = node.callee) === null || _a === void 0 ? void 0 : _a.name) === 'require') {
1453
+ isCommonJS = true;
1454
+ }
1455
+ },
1456
+ ImportDeclaration(node) {
1457
+ isESModule = true;
1458
+ },
1459
+ ExportAllDeclaration(node) {
1460
+ isESModule = true;
1461
+ },
1462
+ ExportDefaultDeclaration(node) {
1463
+ isESModule = true;
1464
+ },
1465
+ ExportNamedDeclaration(node) {
1466
+ isESModule = true;
1467
+ },
1468
+ ExportSpecifier(node) {
1469
+ isESModule = true;
1470
+ },
1471
+ });
1472
+ if (isESModule) {
1473
+ return 'EsModule';
1474
+ }
1475
+ if (isCommonJS) {
1476
+ return 'CommonJS';
1477
+ }
1478
+ return 'Unknown';
1479
+ }
1480
+ catch (error) {
1481
+ console.error(error);
1482
+ return 'Unknown';
1483
+ }
1484
+ }
1418
1485
  async function sortProjectJson(projectPath) {
1419
1486
  try {
1420
1487
  const pkg = await getProjectJson(projectPath);
@@ -1822,7 +1889,7 @@ module.exports = function (plop) {
1822
1889
  type: 'confirm',
1823
1890
  name: 'isRemoveYarn',
1824
1891
  message: '是否移除 yarn ?',
1825
- default: false,
1892
+ default: true,
1826
1893
  when(answers) {
1827
1894
  return answers.isOpenSource;
1828
1895
  },
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.0",
4
4
  "description": "草梅友仁自制的项目模板创建器",
5
5
  "author": "CaoMeiYouRen",
6
6
  "license": "MIT",
@@ -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:
@@ -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
  }