Skip to content

Commit

Permalink
Install cli option
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoobes committed Jan 9, 2024
1 parent ecdd0bc commit 2ab1f40
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules/
dist/
.yarn/cache/
.yarn/install-state.gz
Binary file removed .yarn/install-state.gz
Binary file not shown.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@
"dependencies": {
"colorette": "^2.0.20",
"minimist": "^1.2.8",
"prompts": "^2.4.2"
"prompts": "^2.4.2",
"semver": "^7.5.4"
},
"devDependencies": {
"@types/minimist": "^1.2.2",
"@types/prompts": "^2.4.4",
"@types/semver": "^7.5.6",
"typescript": "^5.0.0"
},
"engines": {
Expand Down
42 changes: 36 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import minimist from 'minimist';
import path from 'path';
import fs from 'fs';
import assert from 'node:assert';
import { spawn, } from 'node:child_process';
import { spawn, execSync } from 'node:child_process';
import { fileURLToPath } from 'node:url';
import semver from 'semver';

const argv = minimist<{
template?: string;
Expand Down Expand Up @@ -100,7 +101,7 @@ async function runInteractive() {
selectedTemplate,
argv.overwrite
);

const installPkgs = await prompt([which_manager], {
onCancel: () => {
console.log('Canceled install '), process.exit(0);
Expand Down Expand Up @@ -211,8 +212,9 @@ function createConfig(isTypescript: boolean) {

async function init() {
console.log(`Working in: ` + cwd);

if (!argv.template) {
await runInteractive();
await runInteractive();
} else {
assert(argv.name);
assert.match(
Expand All @@ -224,9 +226,37 @@ async function init() {
const usebuild = argv['no-build'] ?? true;
await runShort(argv.template, argv.name, usebuild, argv.install);
}
console.log(
magentaBright('Done!')
const installCli = await prompt([{
type: 'confirm',
name: 'installCli',
message: 'install cli? (If you installed a template with `cli`, This is required!!)\n',
initial: true
}])
if(installCli) {
const jsonDeps = JSON.parse(String(execSync('npm ls -g --json')));
const cliVersion = jsonDeps.dependencies['@sern/cli']?.version;
if(semver.satisfies(cliVersion, '1.x')) {
console.log('You already have a good enough sern cli.');
} else {
console.log(`Installing ${magentaBright('@sern/cli')}:`)
await new Promise((resolve, reject) => {
spawn('npm', ['install', '-g', '@sern/cli@latest'], { stdio: 'inherit', cwd, shell: true });
process.on('data', (s) => console.log(s.toString()));
process.on('error', (e) => {
console.error(e);
console.log(red('Something went wrong with installing. Please do it yourself.'));
reject();
});
process.on('exit', resolve)
})


}

}
console.log(magentaBright('Done!')
+ ' visit https://sern.dev for documentation and join https://sern.dev/discord! Happy hacking :)');

}

function emptyDir(dir: string) {
Expand All @@ -245,7 +275,7 @@ async function copyFolderRecursiveAsync(source: string, target: string) {
try {
// Create target folder if it doesn't exist
if (!fs.existsSync(target)) {
fs.mkdirSync(target);
fs.mkdirSync(target);
}

// Get all files and folders in the source folder
Expand Down
36 changes: 36 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ __metadata:
dependencies:
"@types/minimist": ^1.2.2
"@types/prompts": ^2.4.4
"@types/semver": ^7.5.6
colorette: ^2.0.20
minimist: ^1.2.8
prompts: ^2.4.2
semver: ^7.5.4
typescript: ^5.0.0
bin:
create-bot: index.js
Expand Down Expand Up @@ -45,6 +47,13 @@ __metadata:
languageName: node
linkType: hard

"@types/semver@npm:^7.5.6":
version: 7.5.6
resolution: "@types/semver@npm:7.5.6"
checksum: 563a0120ec0efcc326567db2ed920d5d98346f3638b6324ea6b50222b96f02a8add3c51a916b6897b51523aad8ac227d21d3dcf8913559f1bfc6c15b14d23037
languageName: node
linkType: hard

"colorette@npm:^2.0.20":
version: 2.0.20
resolution: "colorette@npm:2.0.20"
Expand All @@ -59,6 +68,15 @@ __metadata:
languageName: node
linkType: hard

"lru-cache@npm:^6.0.0":
version: 6.0.0
resolution: "lru-cache@npm:6.0.0"
dependencies:
yallist: ^4.0.0
checksum: f97f499f898f23e4585742138a22f22526254fdba6d75d41a1c2526b3b6cc5747ef59c5612ba7375f42aca4f8461950e925ba08c991ead0651b4918b7c978297
languageName: node
linkType: hard

"minimist@npm:^1.2.8":
version: 1.2.8
resolution: "minimist@npm:1.2.8"
Expand All @@ -76,6 +94,17 @@ __metadata:
languageName: node
linkType: hard

"semver@npm:^7.5.4":
version: 7.5.4
resolution: "semver@npm:7.5.4"
dependencies:
lru-cache: ^6.0.0
bin:
semver: bin/semver.js
checksum: 12d8ad952fa353b0995bf180cdac205a4068b759a140e5d3c608317098b3575ac2f1e09182206bf2eb26120e1c0ed8fb92c48c592f6099680de56bb071423ca3
languageName: node
linkType: hard

"sisteransi@npm:^1.0.5":
version: 1.0.5
resolution: "sisteransi@npm:1.0.5"
Expand All @@ -102,3 +131,10 @@ __metadata:
checksum: 6f0a9dca6bf4ce9dcaf4e282aade55ef4c56ecb5fb98d0a4a5c0113398815aea66d871b5611e83353e5953a19ed9ef103cf5a76ac0f276d550d1e7cd5344f61e
languageName: node
linkType: hard

"yallist@npm:^4.0.0":
version: 4.0.0
resolution: "yallist@npm:4.0.0"
checksum: 343617202af32df2a15a3be36a5a8c0c8545208f3d3dfbc6bb7c3e3b7e8c6f8e7485432e4f3b88da3031a6e20afa7c711eded32ddfb122896ac5d914e75848d5
languageName: node
linkType: hard

0 comments on commit 2ab1f40

Please sign in to comment.