Skip to content

Commit d5479cb

Browse files
committed
feat: silent option
Added --silent option, to create package without use interaction
1 parent 7cf8fa5 commit d5479cb

File tree

4 files changed

+52
-36
lines changed

4 files changed

+52
-36
lines changed

cli.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,18 @@ const showEnvInfo = async () => {
2424
cli
2525
.command("[out-dir]", "Generate in a custom directory or current directory")
2626
.option(
27-
"-i",
28-
"--info",
27+
"-i, --info",
2928
"Print out debugging information relating to the local environment"
3029
)
31-
.option("-v", "--verbose", "Show debug logs")
30+
.option(
31+
"-s, --silent",
32+
"Silent mode. Create package without user interaction"
33+
)
34+
.option("--no-travis", "Don't use travis ci.")
35+
.option("--no-tests", "Don't use tests")
36+
.option("--no-semantic-release", "Don't use semantic release")
37+
.option("--npm", "Use NPM package manager. Default is YARN")
38+
.option("--verbose", "Show debug logs")
3239
.action((outDir = ".", cliOptions) => {
3340
if (cliOptions.info) {
3441
return showEnvInfo();
@@ -37,9 +44,9 @@ cli
3744
console.log(chalk`{cyan @epranka/create-tsx-package v${version}}`);
3845
console.log(chalk`✨ Generating TSX package in {cyan ${outDir}}`);
3946

40-
const { verbose } = cliOptions;
47+
const { verbose, silent } = cliOptions;
4148
const logLevel = verbose ? 4 : 2;
42-
sao({ generator, outDir, logLevel, cliOptions })
49+
sao({ generator, outDir, logLevel, cliOptions, yes: silent })
4350
.run()
4451
.catch(err => {
4552
console.trace(err);

prompts.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
const getRepositoryURL = require("./utils/repository");
22
const { random } = require("superb");
33

4+
const args = process.argv;
5+
const useTravis = !args.includes("--no-travis");
6+
const useTests = !args.includes("--no-tests");
7+
const useSemanticRelease = !args.includes("--no-semantic-release");
8+
const useNPM = args.includes("--npm");
9+
410
module.exports = [
511
{
612
name: "name",
@@ -20,31 +26,29 @@ module.exports = [
2026
{ name: "Npm", value: "npm" }
2127
],
2228
type: "list",
23-
default: "yarn"
29+
default: useNPM ? "npm" : "yarn"
2430
},
2531
{
2632
name: "tests",
2733
message: "Use unit tests ?",
2834
type: "confirm",
29-
default: true
35+
default: useTests
3036
},
3137
{
3238
name: "semanticrelease",
3339
message: "Use automatic semantic releases ?",
3440
type: "confirm",
35-
default: true
41+
default: useSemanticRelease
3642
},
3743
{
3844
name: "travis",
3945
message: "Use Travis ?",
4046
type: "confirm",
41-
default: true
47+
default: useTravis
4248
},
4349
{
4450
name: "repository",
4551
message: "Respository URL",
46-
default: async () => {
47-
return await getRepositoryURL();
48-
}
52+
default: getRepositoryURL()
4953
}
5054
];

saofile.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ const spawn = require("cross-spawn");
22
const validate = require("validate-npm-package-name");
33
const serializePackage = require("./utils/serializePackage");
44
const serializeTSConfig = require("./utils/serializeTSConfig");
5+
const prompts = require("./prompts");
56

67
module.exports = {
7-
prompts: require("./prompts"),
8+
prompts: prompts,
89
templateData() {
910
const tsconfig = {
1011
compilerOptions: {
@@ -147,7 +148,9 @@ module.exports = {
147148
};
148149
},
149150
actions() {
150-
const validation = validate(this.answers.name);
151+
const validation = validate(
152+
(this.answers && this.answers.name) || this.outFolder
153+
);
151154
validation.warnings &&
152155
validation.warnings.forEach(warn => {
153156
console.warn("Warning:", warn);
@@ -191,9 +194,11 @@ module.exports = {
191194
return actions;
192195
},
193196
async completed() {
197+
const { cliOptions } = this.sao.opts;
198+
const silent = cliOptions.silent;
194199
this.gitInit();
195200
await this.npmInstall({ npmClient: this.answers.pm });
196-
if (this.answers.semanticrelease) {
201+
if (!silent && this.answers.semanticrelease) {
197202
// @TODO console log
198203
// install semantic release cli
199204
let options = ["add", "semantic-release-cli"];
@@ -218,6 +223,9 @@ module.exports = {
218223
cwd: this.outDir,
219224
stdio: "inherit"
220225
});
226+
}
227+
228+
if (this.answers.semanticrelease) {
221229
// Setup commitizen
222230
spawn.sync(
223231
"./node_modules/.bin/commitizen",

utils/repository.js

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,24 @@
11
const fs = require("fs");
22

33
const getRepositoryURL = () => {
4-
return new Promise((resolve, reject) => {
5-
fs.readFile(".git/config", "utf-8", (err, gitconf) => {
6-
if (err) return reject(err);
7-
if (!gitconf) return "";
8-
gitconf = gitconf.split(/\r?\n/);
9-
var i = gitconf.indexOf('[remote "origin"]');
10-
if (i !== -1) {
11-
var u = gitconf[i + 1];
12-
if (!u.match(/^\s*url =/)) u = gitconf[i + 2];
13-
if (!u.match(/^\s*url =/)) u = null;
14-
else u = u.replace(/^\s*url = /, "");
15-
}
16-
if (u && u.match(/^git@github.com:/)) {
17-
u = u.replace(/^git@github.com:/, "https://github.com/");
18-
}
19-
if (!u) {
20-
return resolve("");
21-
}
22-
return resolve(u);
23-
});
24-
});
4+
let gitconf = fs.readFileSync(".git/config", "utf-8");
5+
if (!gitconf) return "";
6+
gitconf = gitconf.split(/\r?\n/);
7+
const i = gitconf.indexOf('[remote "origin"]');
8+
let u;
9+
if (i !== -1) {
10+
u = gitconf[i + 1];
11+
if (!u.match(/^\s*url =/)) u = gitconf[i + 2];
12+
if (!u.match(/^\s*url =/)) u = null;
13+
else u = u.replace(/^\s*url = /, "");
14+
}
15+
if (u && u.match(/^git@github.com:/)) {
16+
u = u.replace(/^git@github.com:/, "https://github.com/");
17+
}
18+
if (!u) {
19+
return "";
20+
}
21+
return u;
2522
};
2623

2724
module.exports = getRepositoryURL;

0 commit comments

Comments
 (0)