Browse Source

added clone command

pull/1/head
Nicolas Beaussart 7 years ago
parent
commit
f938797086
  1. 6
      package.json
  2. 91
      src/commands/clone.ts
  3. 2
      src/commands/wallhaven/wallhaven.ts
  4. 37
      src/extensions/clone.ts
  5. 1072
      yarn.lock

6
package.json

@ -1,6 +1,6 @@
{ {
"name": "@nicolasbeaussart/nbx", "name": "@nicolasbeaussart/nbx",
"version": "0.0.1",
"version": "0.0.2",
"description": "nbx CLI", "description": "nbx CLI",
"private": false, "private": false,
"bin": { "bin": {
@ -16,7 +16,8 @@
"test": "jest", "test": "jest",
"watch": "jest --watch", "watch": "jest --watch",
"snapupdate": "jest --updateSnapshot", "snapupdate": "jest --updateSnapshot",
"coverage": "jest --coverage"
"coverage": "jest --coverage",
"release": "release-it"
}, },
"files": [ "files": [
"tsconfig.json", "tsconfig.json",
@ -39,6 +40,7 @@
"@types/node": "^10.12.12", "@types/node": "^10.12.12",
"jest": "^23.6.0", "jest": "^23.6.0",
"prettier": "^1.12.1", "prettier": "^1.12.1",
"release-it": "^12.3.0",
"ts-jest": "^23.10.5", "ts-jest": "^23.10.5",
"tslint": "^5.12.0", "tslint": "^5.12.0",
"tslint-config-prettier": "^1.17.0", "tslint-config-prettier": "^1.17.0",

91
src/commands/clone.ts

@ -0,0 +1,91 @@
import { GluegunToolbox } from 'gluegun'
module.exports = {
name: 'clone',
alias: ['c'],
run: async (toolbox: GluegunToolbox) => {
const {
parameters,
print: { info, error, spin },
utils: { verboseDebug },
prompt: { ask },
clone: { getWorkScopes, cloneUrlInScope, getCloneDirectory }
} = toolbox
const showHelp = () => {
info('Usage: nbx clone [url]')
info('')
info('Options')
info(' -s, --scope Scope')
info(' -t, --type Type (git, expr, doc)')
info(' -h, --help Output usage information')
info(' -v, --verbose Verbose output')
}
const o = parameters.options
let props = {
url: parameters.first,
scope: o.s || o.scope,
type: o.t || o.type,
help: Boolean(o.h || o.help),
verbose: Boolean(o.v || o.verbose)
}
verboseDebug(props, 'Params')
if (props.help) {
showHelp()
return
}
if (!props.url) {
error('You must provide a url to clone\n')
showHelp()
process.exit(0)
}
let questions = []
if (!props.type) {
questions = [
...questions,
{
choices: ['git', 'expr', 'docs'],
default: 'git',
name: 'type',
message: 'Witch type of project are you gonna clone ?',
type: 'list'
}
]
}
if (!props.scope) {
const workScopes = await getWorkScopes()
questions = [
...questions,
{
choices: ['perso', 'fac', ...workScopes],
default: 'perso',
name: 'scope',
message: 'In what scope do you want to clone into',
type: 'list'
}
]
}
if (questions.length) {
const res = await ask(questions)
props = {
...props,
...res
}
}
verboseDebug(props, 'Params')
const dir = await getCloneDirectory(props.scope, props.type);
const spinner = spin(`Cloning into ${dir}`)
try {
const path = await cloneUrlInScope(props.url, dir)
spinner.succeed('Cloned into ' + path);
} catch (e) {
spinner.fail();
}
}
}

2
src/commands/wallhaven/wallhaven.ts

@ -15,7 +15,7 @@ module.exports = {
const showHelp = () => { const showHelp = () => {
info('Usage: wallhaven-cli <terms ...>')
info('UsageUsage: nbx wallhaven <terms ...>')
info('') info('')
info('Options') info('Options')
info(' -r, --random Pick one randomly') info(' -r, --random Pick one randomly')

37
src/extensions/clone.ts

@ -0,0 +1,37 @@
import { GluegunToolbox } from 'gluegun'
module.exports = (toolbox: GluegunToolbox) => {
const getWorkScopes = async (): Promise<string[]> => {
const files = await toolbox.filesystem.list(toolbox.filesystem.homedir() + toolbox.filesystem.separator + 'work');
return files || [];
}
const cloneUrlInScope = async (gitUrl: string, directory: string): Promise<string> => {
const folderBefore = (await toolbox.filesystem.list(directory)) as string[];
await toolbox.system.exec(`git clone ${gitUrl}`, { cwd: directory })
const folderAfter = (await toolbox.filesystem.list(directory)) as string[];
const newFolder = folderAfter.filter(val => !folderBefore.includes(val));
toolbox.utils.verboseDebug(newFolder, 'newFolder ?');
return directory + toolbox.filesystem.separator + newFolder[0];
}
const getCloneDirectory = async (scope: string, type: string): Promise<string> => {
const endPart = scope + toolbox.filesystem.separator + type;
let directory = toolbox.filesystem.homedir() + toolbox.filesystem.separator + 'work' + toolbox.filesystem.separator + endPart;
if (['fac', 'perso'].includes(scope)) {
directory = toolbox.filesystem.homedir() + scope + toolbox.filesystem.separator + endPart;
}
return directory;
}
toolbox.clone = {
getWorkScopes,
cloneUrlInScope,
getCloneDirectory,
}
}

1072
yarn.lock
File diff suppressed because it is too large
View File

Loading…
Cancel
Save