2022-12-21 13:17:50 +00:00
|
|
|
const Myt = require('./myt');
|
2022-12-21 12:34:17 +00:00
|
|
|
const Command = require('./lib/command');
|
2020-12-02 07:35:26 +00:00
|
|
|
const fs = require('fs-extra');
|
2022-12-29 16:58:40 +00:00
|
|
|
const path = require('path');
|
2020-12-02 07:35:26 +00:00
|
|
|
|
2022-12-21 12:34:17 +00:00
|
|
|
class Init extends Command {
|
|
|
|
static usage = {
|
|
|
|
description: 'Initialize an empty workspace'
|
|
|
|
};
|
2021-10-25 13:38:07 +00:00
|
|
|
|
2022-12-21 13:17:50 +00:00
|
|
|
async run(myt, opts) {
|
2022-12-29 16:58:40 +00:00
|
|
|
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) {
|
2022-12-29 16:58:40 +00:00
|
|
|
const dst = path.join(opts.mytDir, template);
|
2020-12-02 07:35:26 +00:00
|
|
|
if (!await fs.pathExists(dst))
|
2022-12-29 16:58:40 +00:00
|
|
|
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)
|
2022-12-21 13:17:50 +00:00
|
|
|
new Myt().run(Init);
|