Skip to content

Commit

Permalink
- Add loading
Browse files Browse the repository at this point in the history
  • Loading branch information
ronaldohoch committed Apr 18, 2021
1 parent 560efdd commit 5adf0e5
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 5 deletions.
6 changes: 6 additions & 0 deletions lib/i18n/i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
"exit_process":"Processo terminou com o código: ",
"errors":{
"not_found":"package.json não encontrado. Você está rodando em um projeto npm?"
},
"loading":{
"packageJson":"Carregando package.json"
}
},
"en-US":{
Expand All @@ -13,6 +16,9 @@
"exit_process":"Child process exited with code: ",
"errors":{
"not_found":"Can't find package.json file. Are You in a npm project?"
},
"loading":{
"packageJson":"Loading package.json"
}
}
}
20 changes: 17 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,41 @@
#!/usr/bin/env node
//node
const path = require("path");
const fs = require("fs");
const spawn = require("child_process").spawn;
//thirds
const inquirer = require("inquirer");
const ora = require("ora");
//this package
const local = require("./get-locale")();
const getFileAsync = require("./get-file-async");

(async () => {
const i18n = ora("🔎 i18n").start();
const data = await getFileAsync(path.join(__dirname, "../lib/i18n/i18n.json"));
let labels = data["en-US"];
if (data.hasOwnProperty(local)) {
labels = data[local];
}
i18n.succeed();
const packageLoading = ora(labels.loading.packageJson).start();

let packageFile, entries, choiceList=[];
try{
packageFile = await getFileAsync(path.join(process.cwd(), "package.json"));
entries = Object.entries(packageFile.scripts);
choiceList = [];
packageLoading.succeed();
}catch(err){
packageLoading.fail();
console.log(labels.errors.not_found); return;
}

entries.forEach(([key, value]) => {
choiceList.push({
name: key,
value: key,
short: `${labels.running} 'npm run ${key}'...`
short: ``
// short: `${labels.running} 'npm run ${key}'...`
});
});
inquirer
Expand All @@ -43,20 +49,28 @@ const getFileAsync = require("./get-file-async");
}
])
.then(answers => {
const commandLoading = ora(`npm run ${answers.scripts}`).start();
let command = spawn(`npm run ${answers.scripts}`, ["--colors"], {
stdio: 'inherit',
shell: true,
});
commandLoading.succeed();
command.stdout.on('data', function (data) {
// commandLoading.succeed();
console.log(data.toString());
});
command.stderr.on('data', function (data) {
// commandLoading.fail();
console.log(data.toString());
});
command.on('exit', function (code) {
// commandLoading.succeed();
console.log(`${labels.exit_process} ${code.toString()}`);
});
command.on('error', function (err) { console.log(err); return; })
command.on('error', function (err) {
// commandLoading.fail();
console.log(err); return;
})
})
.catch(error => { });
})()
125 changes: 124 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
},
"homepage": "https://github.com/ronaldohoch/what-script-run#readme",
"dependencies": {
"inquirer": "^8.0.0"
"inquirer": "^8.0.0",
"ora": "^5.4.0"
}
}

0 comments on commit 5adf0e5

Please sign in to comment.