myt/myt-init.js

37 lines
1.1 KiB
JavaScript
Raw Normal View History

2022-12-21 13:17:50 +00:00
const Myt = require('./myt');
const Command = require('./lib/command');
2020-12-02 07:35:26 +00:00
const fs = require('fs-extra');
const path = require('path');
2020-12-02 07:35:26 +00:00
class Init extends Command {
static usage = {
description: 'Initialize an empty workspace'
};
2022-12-21 13:17:50 +00:00
async run(myt, opts) {
const packageFile = path.join(opts.mytDir, 'package.json');
const packageExists = await fs.pathExists(packageFile);
const templateDir = path.join(__dirname, 'template');
2020-12-02 07:35:26 +00:00
const templates = await fs.readdir(templateDir);
for (let template of templates) {
const dst = path.join(opts.mytDir, template);
2020-12-02 07:35:26 +00:00
if (!await fs.pathExists(dst))
await fs.copy(path.join(templateDir, template), dst);
}
if (!packageExists) {
const packageJson = require(packageFile);
packageJson.dependencies[myt.packageJson.name] =
`^${myt.packageJson.version}`;
await fs.writeFile(packageFile,
JSON.stringify(packageJson, null, ' '));
2020-12-02 07:35:26 +00:00
}
}
}
module.exports = Init;
if (require.main === module)
new Myt().cli(Init);