實現了個輸入命令自動打包提交git的node腳本,將用過的點子收集了一下:
///// dabao.js
const {execSync} = require('child_process'); //執行腳本代碼
const chalk = require('chalk'); //顏色插件
const inquirer = require('inquirer'); //用戶交互插件
const os = require('os'); //操作系統
const ora = require('ora'); //狀態樣式插件
const fs = require('fs'); //文件系統
process.argv.map((item)=>{ //process.argv獲取指令參數 如 node dabao.js ok -haha 循環結果:1.node執行程序地址 2.被執行文件地址 3~.指令參數
console.log(item)
if(item.indexOf('ok') > -1){
console.log('okok')
}
})
const spinner = ora('Loading unicorns').start(); //交互界面狀態組件
setTimeout(() => {
spinner.color = 'yellow';
spinner.text = 'Loading rainbows';
}, 1000);
setTimeout(() => {
spinner.stop()
},2500)
Dabaole = async function Dabao() {
await fs.mkdir('./OMG') //操作文件系統,建文件夾寫文件
await fs.writeFileSync('./OMG/寫我.js', '成都國際車展;', 'utf8');
console.log(321)
console.log(os.type())//獲得操作系統
console.log(chalk.red('im ok...\n'));
result = await inquirer.prompt([//用戶界面交互
{
type: 'confirm',
name: 'confirm',
message: 'are you OK?',
},
{
type: 'rawlist',
name: 'gender',
message: 'male or female?',
choices: [
'im mal',
'im female'
]
}
]);
await execSync(`git status`)//////做你想做的 可以做很多事
}
Dabaole()
運行 node dabao.js ok -haha
,執行效果如下
node.js api詳詢 http://nodejs.cn/api/