This repository has been archived by the owner on Jun 20, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Working CLI (install skeletons)
- Loading branch information
Showing
7 changed files
with
192 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import * as log from "./log"; | ||
|
||
export class Help { | ||
constructor(args) { | ||
log.help({ | ||
"install --search [name]" : "Searches for a skeleton (or lists them)", | ||
"install [name]" : "Install skeleton into the current folder", | ||
"install [name] [dir]" : "Install skeleton into the specific directory", | ||
"install --update" : "Updates the skeleton repository", | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import * as path from "path"; | ||
import * as fsExtra from "fs-extra"; | ||
import * as fs from "fs"; | ||
import { exec } from "child_process"; | ||
import * as log from "./log"; | ||
|
||
const HOME = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE; | ||
const REPOSITORY_PATH = path.join(HOME, '.fuse-box', 'bootstrap-collection'); | ||
const SKELETONS = path.join(REPOSITORY_PATH, 'skeletons'); | ||
function listProjects(dirname) { | ||
const results = fs.readdirSync(dirname).filter(function (file) { | ||
return fs.statSync(path.join(dirname, file)).isDirectory(); | ||
}); | ||
const output = []; | ||
results.forEach(dir => { | ||
const absPath = path.join(dirname, dir); | ||
const packageJSON = path.join(absPath, "package.json"); | ||
if (fs.existsSync(packageJSON)) { | ||
const json = require(packageJSON); | ||
output.push({ dir: absPath, json: json }) | ||
} | ||
}); | ||
return output; | ||
} | ||
|
||
export class Install { | ||
constructor(args) { | ||
const major = args._; | ||
if (major.length === 0) { | ||
if (args.update) { | ||
this.update(); | ||
} else { | ||
this.search(args.search); | ||
} | ||
} else { | ||
this.install(major[0], major[1]) | ||
} | ||
} | ||
|
||
private async update() { | ||
await this.verifyRepository() | ||
await this.pullRepository(); | ||
} | ||
|
||
private async pullRepository() { | ||
return new Promise((resolve, reject) => { | ||
fsExtra.ensureDirSync(REPOSITORY_PATH); | ||
log.info("pulling https://github.com/fuse-box/bootstrap-collection") | ||
exec("git pull", | ||
{ cwd: REPOSITORY_PATH }, | ||
(error, stdout, stderr) => { | ||
if (error) { | ||
return reject(error); | ||
} | ||
log.info("Pulled successfully") | ||
return resolve(); | ||
}) | ||
}); | ||
} | ||
private cloneRepository() { | ||
return new Promise((resolve, reject) => { | ||
fsExtra.ensureDirSync(REPOSITORY_PATH); | ||
log.info("Cloning https://github.com/fuse-box/bootstrap-collection") | ||
exec("git clone https://github.com/fuse-box/bootstrap-collection .", | ||
{ cwd: REPOSITORY_PATH }, | ||
(error, stdout, stderr) => { | ||
if (error) { | ||
return reject(error); | ||
} | ||
log.info("Cloned successfully") | ||
return resolve(); | ||
}) | ||
}); | ||
|
||
} | ||
private async verifyRepository() { | ||
if (!fs.existsSync(REPOSITORY_PATH)) { | ||
log.info("Repository not found") | ||
this.cloneRepository(); | ||
} | ||
} | ||
|
||
private async search(searchName?: string) { | ||
log.info("Listing available skeletons") | ||
await this.verifyRepository(); | ||
const projects = listProjects(SKELETONS); | ||
const results = projects.filter(item => { | ||
if (typeof searchName !== "string") return item; | ||
if (item.json.name.toLowerCase().indexOf(searchName.toLowerCase()) > -1) { | ||
return item; | ||
} | ||
}); | ||
results.forEach(item => { | ||
log.title(item.json.name); | ||
log.desc(item.json.description); | ||
}) | ||
} | ||
|
||
private async install(name: string, targetPath: string) { | ||
log.info(`Installing ${name}`); | ||
const projects = listProjects(SKELETONS); | ||
const result = projects.filter(item => { | ||
if (item.json.name.toLowerCase().indexOf(name.toLowerCase()) > -1) { | ||
return item; | ||
} | ||
}); | ||
if (!result.length) { | ||
return log.error("Skeleton was not found. Try 'fuse install --search'") | ||
} | ||
if (result.length > 1) { | ||
return log.error("More than one repository found") | ||
} | ||
const item = result[0]; | ||
const source = item.dir; | ||
targetPath = path.join(process.cwd(), targetPath ? targetPath : item.json.name); | ||
if (fs.existsSync(targetPath)) { | ||
log.error(`Directory ${targetPath} exists`); | ||
log.error(`Choose a different one by typing:`) | ||
log.info(`fuse install "${name}" dirname`); | ||
return; | ||
} | ||
log.info(`Copying to ${targetPath}`); | ||
await fsExtra.copy(source, targetPath) | ||
log.info(`Done!`); | ||
log.info(` cd ${targetPath};`); | ||
log.info(` yarn;`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,25 @@ | ||
#!/usr/bin/env node | ||
#! /usr/bin/env node | ||
const argv = require('minimist')(process.argv.slice(2)); | ||
import { Install } from './Install'; | ||
import { Help } from './Help'; | ||
|
||
import * as yargs from "yargs"; | ||
import { FuseFileExecution } from './FuseFileExecution'; | ||
|
||
const SYSTEM_ARGS = ["boostrap", "snippet", "run"]; | ||
const CMD = { | ||
"install": Install, | ||
'help': Help | ||
} | ||
function extractParams(args) { | ||
|
||
function extractParams(args){ | ||
const primaryArgs = args._; | ||
const firstPrimaryArg = primaryArgs[0]; | ||
return { | ||
primary : firstPrimaryArg, | ||
args : args, | ||
isSystemArgument : () => SYSTEM_ARGS.indexOf(firstPrimaryArg) > -1 | ||
if (args.help) { | ||
return new Help(args) | ||
} | ||
args._.forEach((name, index) => { | ||
if (CMD[name]) { | ||
args._ = args._.splice(index + 1) | ||
return new CMD[name](args) | ||
} | ||
}); | ||
} | ||
function initCLI() { | ||
const args = extractParams(yargs.args); | ||
if( !args.primary || !args.isSystemArgument() && FuseFileExecution.test()){ | ||
return FuseFileExecution.init(args); | ||
} | ||
extractParams(argv); | ||
} | ||
initCLI(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import * as chalk from "chalk"; | ||
|
||
export function info(text: string) { | ||
console.log(chalk.gray(` : ${text}`)); | ||
} | ||
export function help(obj: any) { | ||
for (const cmd in obj) { | ||
console.log(' ' + chalk.green.bold(cmd)); | ||
console.log(chalk.gray(` ${obj[cmd]}`)); | ||
} | ||
} | ||
export function error(text: string) { | ||
console.log(chalk.bold.red(` : ${text}`)); | ||
} | ||
export function title(text: string) { | ||
console.log(chalk.bold.green(` ${text}`)); | ||
} | ||
export function desc(text: string) { | ||
console.log(chalk.gray(` - ${text}`)); | ||
} |