tiddlywiki-plugin-dev
Version:
[](https://github.com/tiddly-gittly)
122 lines (120 loc) • 4.84 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.init = void 0;
var _fs = _interopRequireDefault(require("fs"));
var _path = _interopRequireDefault(require("path"));
var _child_process = require("child_process");
var _chalk = _interopRequireDefault(require("chalk"));
var _inquirer = _interopRequireDefault(require("inquirer"));
var _simpleGit = _interopRequireDefault(require("simple-git"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const init = async (project, githubUrl, npmUrl) => {
if (_fs.default.existsSync(project)) {
console.error(`${project} already exists!`);
return;
}
const {
npm,
authorName
} = await _inquirer.default.prompt([{
type: 'list',
name: 'npm',
message: 'Which package manager do you use?',
choices: ['npm', 'yarn', 'pnpm', 'tnpm', 'cnpm'],
default: 'npm'
}, {
type: 'input',
name: 'authorName',
message: "What's your name ($:/plugins/<fill in your name>/plugin-name)"
}]);
const {
pluginName
} = await _inquirer.default.prompt([{
type: 'input',
name: 'pluginName',
message: `What's the plugin's name ($:/plugins/${authorName}/<fill in plugin name>)`
}]);
// eslint-disable-next-line no-console
console.log(_chalk.default.green.bold(`Cloning template project from ${githubUrl}`));
// pull template
await (0, _simpleGit.default)().clone(githubUrl, project, ['-b', 'template']);
const git = (0, _simpleGit.default)({
baseDir: _path.default.resolve(project)
});
// 修改 git 信息
await git.removeRemote('origin');
await git.branch(['-m', 'template', 'master']);
const shallowPath = _path.default.resolve(project, '.git', 'shallow');
if (_fs.default.existsSync(shallowPath)) {
_fs.default.rmSync(_path.default.resolve(project, '.git', 'shallow'));
}
// npm 镜像
if (npmUrl) {
_fs.default.writeFileSync(_path.default.resolve(project, '.npmrc'), [..._fs.default.readFileSync(_path.default.resolve(project, '.npmrc'), 'utf-8').split('\n'), `registry=${npmUrl}`].map(line => line.trim()).filter(line => line !== '').join('\n'));
}
// 安装
(0, _child_process.execSync)(`${npm} install`, {
cwd: _path.default.resolve(project),
stdio: 'inherit'
});
// 更新 npm 依赖
(0, _child_process.execSync)(`${npm} run update`, {
cwd: _path.default.resolve(project),
stdio: 'inherit'
});
// 安装
(0, _child_process.execSync)(`${npm} install`, {
cwd: _path.default.resolve(project),
stdio: 'inherit'
});
// CI 脚本修改
const ciPath = _path.default.resolve(project, '.github', 'workflows');
if (_fs.default.existsSync(ciPath)) {
for (const file of _fs.default.readdirSync(ciPath)) {
if (_path.default.extname(file) !== '.yml') {
continue;
}
const filePath = _path.default.resolve(ciPath, file);
const content = _fs.default.readFileSync(filePath, 'utf-8').replace(/npm\s+install\s+-g\s+pnpm\s+&&\s+/g, npm === 'npm' ? '' : `npm install -g ${npm} && `).replace(/pnpm\s+install/g, `${npm} install`).replace(/pnpm\s+run/g, `${npm} run`);
_fs.default.writeFileSync(filePath, content, 'utf-8');
}
}
// 更新 dprint 依赖
(0, _child_process.execSync)(`npx dprint config update`, {
cwd: _path.default.resolve(project),
stdio: 'inherit'
});
// 更新模板里的占位符插件名
const nameFrom = '$:/plugins/your-name/plugin-name';
const nameTo = `$:/plugins/${authorName}/${pluginName}`;
const pluginPathFrom = _path.default.resolve(project, 'src', 'plugin-name');
const pluginPathTo = _path.default.resolve(project, 'src', pluginName);
replaceStringInFilesSync(pluginPathFrom, nameFrom, nameTo);
_fs.default.renameSync(pluginPathFrom, pluginPathTo);
replaceStringInFilesSync(_path.default.resolve(project, 'wiki'), nameFrom, nameTo);
};
/**
* Synchronously replaces all occurrences of a specified string in all files of a folder, including subdirectories.
*
* @param {string} dir - The directory to search in.
* @param {string} searchString - The string to search for.
* @param {string} replaceString - The string to replace with.
*/
exports.init = init;
function replaceStringInFilesSync(dir, searchString, replaceString) {
const files = _fs.default.readdirSync(dir);
files.forEach(file => {
const filePath = _path.default.join(dir, file);
const stats = _fs.default.statSync(filePath);
if (stats.isFile()) {
const data = _fs.default.readFileSync(filePath, 'utf8');
const result = data.replaceAll(searchString, replaceString);
_fs.default.writeFileSync(filePath, result, 'utf8');
} else if (stats.isDirectory()) {
// Recurse into the subdirectory
replaceStringInFilesSync(filePath, searchString, replaceString);
}
});
}