You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
87 lines
2.5 KiB
87 lines
2.5 KiB
const fs = require('fs'); |
|
const path = require('path'); |
|
|
|
function getArgs (startIndex = 1) { |
|
const args = {}; |
|
process.argv |
|
.slice(startIndex, process.argv.length) |
|
.forEach(arg => { |
|
// long arg |
|
if (arg.slice(0, 2) === '--') { |
|
const longArg = arg.split('='); |
|
const longArgFlag = longArg[0].slice(2, longArg[0].length); |
|
const longArgValue = longArg.length > 1 ? longArg[1] : true; |
|
args[longArgFlag] = longArgValue; |
|
// flags |
|
} else if (arg[0] === '-') { |
|
const flags = arg.slice(1, arg.length).split(''); |
|
flags.forEach(flag => { |
|
args[flag] = true; |
|
}); |
|
} |
|
}); |
|
|
|
return args; |
|
} |
|
|
|
function scanAndCreate(structure, root = process.cwd()) { |
|
Object.keys(structure) |
|
.forEach(name => { |
|
if (typeof structure[name] === 'object') { |
|
fs.mkdirSync(path.resolve(root, name)); |
|
|
|
scanAndCreate(structure[name], path.resolve(root, `./${name}`)); |
|
} else if (structure[name] === '') { |
|
fs.appendFileSync(path.resolve(root, name), ''); |
|
} else if (typeof structure[name] === 'string') { |
|
const content = fs.readFileSync(structure[name]).toString(); |
|
|
|
fs.appendFileSync(path.resolve(root, name), content); |
|
} |
|
}); |
|
} |
|
|
|
module.exports = { |
|
exec: async () => { |
|
const args = getArgs(process.argv); |
|
|
|
if (!args['-init']) { |
|
throw new Error('command not found'); |
|
} |
|
|
|
if (!args['--name']) { |
|
throw new Error('fileName not found'); |
|
} |
|
|
|
const name = args['--name']; |
|
|
|
const structure = { |
|
[`${name}_worker`]: { |
|
'main_thread': { |
|
action: {}, |
|
[`${name}_main_thread.ts`]: path.resolve(__dirname, './template/main_thread_template.ts'), |
|
}, |
|
utils: { |
|
'action_type.ts': '', |
|
}, |
|
'worker_thread': { |
|
action: {}, |
|
[`${name}_worker_thread.ts`]: path.resolve(__dirname, './template/worker_thread_template.ts'), |
|
}, |
|
}, |
|
}; |
|
|
|
scanAndCreate(structure); |
|
}, |
|
}; |
|
|
|
// 结构 |
|
// -xxx_worker |
|
// -|--main_thread |
|
// -|--|--action |
|
// -|--|--xxx_main_thread.ts |
|
// -|--utils |
|
// -|--|--action_type.ts |
|
// -|--worker_thread |
|
// -|--|--action |
|
// -|--|--worker_main_thread.ts
|
|
|