Skip to content
This repository has been archived by the owner on Jun 20, 2023. It is now read-only.

Commit

Permalink
feat: Working CLI (install skeletons)
Browse files Browse the repository at this point in the history
  • Loading branch information
nchanged committed Jun 12, 2018
1 parent 8d49560 commit 87df46a
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 16 deletions.
7 changes: 7 additions & 0 deletions gulpfile.js
Expand Up @@ -156,6 +156,13 @@ gulp.task("dist", ["prepare:clean"], function(done) {
done)
});

gulp.task("dev-cli", [], function(done) {
runSequence("prepare:js")
return result = gulp.watch("src/**/**", done => {
return runSequence("prepare:js")
})
});

gulp.task("prepare:es6-index", () => {
const contents = `
const path = require("path");
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -87,6 +87,7 @@
"ieee754": "^1.1.8",
"inquirer": "^3.0.6",
"lego-api": "^1.0.7",
"minimist": "^1.2.0",
"mustache": "^2.3.0",
"node-sass": "^4.9.0",
"postcss": "^6.0.1",
Expand Down
6 changes: 6 additions & 0 deletions src/cli/FuseFileExecution.ts
Expand Up @@ -4,7 +4,13 @@ export class FuseFileExecution {
public static test(){
console.log(process.cwd());
}

public install(){
console.log(this.args);
console.log("install");
}
public static init(args : any){
console.log("init", args);
return new FuseFileExecution(args);
}
}
12 changes: 12 additions & 0 deletions src/cli/Help.ts
@@ -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",
})
}
}
128 changes: 128 additions & 0 deletions src/cli/Install.ts
@@ -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;`);
}
}
34 changes: 18 additions & 16 deletions src/cli/entry.ts
@@ -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();
20 changes: 20 additions & 0 deletions src/cli/log.ts
@@ -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}`));
}

0 comments on commit 87df46a

Please sign in to comment.