Skip to content
This repository has been archived by the owner on Feb 5, 2022. It is now read-only.

Commit

Permalink
feat(cli): allow to set an alias name for a generator, added set-alia…
Browse files Browse the repository at this point in the history
…s get-alias commands.
  • Loading branch information
egoist committed Nov 13, 2018
1 parent d005ca6 commit 4fc26cc
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 4 deletions.
23 changes: 23 additions & 0 deletions bin/cli.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env node
const cac = require('cac').default
const SAOError = require('../lib/SAOError')

const cli = cac()

Expand Down Expand Up @@ -41,6 +42,28 @@ cli
alias: 'y'
})

cli.command('set-alias', 'Set an alias for a generator path', input => {
const store = require('../lib/store')
const { escapeDots } = require('../lib/utils/common')
const logger = require('../lib/logger')

const name = input[0]
const value = input[1]
if (!name || !value) {
throw new SAOError(`Invalid arguments: sao set-alias <alias> <generator>`)
}

store.set(`alias.${escapeDots(name)}`, value)
logger.success(`Added alias '${name}'`)
})

cli.command('get-alias', 'Get the generator for an alias', input => {
const store = require('../lib/store')
const { escapeDots } = require('../lib/utils/common')

console.log(store.get(`alias.${escapeDots(input[0])}`))
})

cli.on('error', error => {
return require('..').handleError(error)
})
Expand Down
2 changes: 1 addition & 1 deletion lib/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Logger {
constructor(options) {
this.options = Object.assign(
{
logLevel: 2
logLevel: 3
},
options
)
Expand Down
39 changes: 37 additions & 2 deletions lib/parseGenerator.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
const path = require('path')
const sum = require('hash-sum')
const paths = require('./paths')
const store = require('./store')
const SAOError = require('./SAOError')
const isLocalPath = require('./utils/isLocalPath')
const { escapeDots } = require('./utils/common')

/**
*
Expand All @@ -24,11 +27,12 @@ module.exports = generator => {
}
}

if (!generator.startsWith('npm:') && !generator.includes('/')) {
const SPECIAL_PREFIX_RE = /^(npm|github|bitbucket|gitlab|alias):/

if (!SPECIAL_PREFIX_RE.test(generator) && !generator.includes('/')) {
generator = `npm:sao-${generator}`
}

const SPECIAL_PREFIX_RE = /^(npm|github|bitbucket|gitlab):/
/** @type {string|null} */
let type = null
if (SPECIAL_PREFIX_RE.test(generator)) {
Expand Down Expand Up @@ -56,6 +60,37 @@ module.exports = generator => {
}
}

if (type === 'alias') {
const hasSubGenerator = generator.indexOf(':') !== -1
const alias = generator.slice(
0,
hasSubGenerator ? generator.indexOf(':') : generator.length
)
const url = store.get(`alias.${escapeDots(alias)}`)
if (!url) {
throw new SAOError(`Cannot find alias '${alias}'`)
}
if (isLocalPath(url)) {
return {
type: 'local',
path: url,
subGenerator:
hasSubGenerator && generator.slice(generator.indexOf(':') + 1),
hash: sum(url)
}
}
const slug = `direct:${url}`
const hash = sum(`repo:${slug}`)
return {
type: 'repo',
slug,
subGenerator:
hasSubGenerator && generator.slice(generator.indexOf(':') + 1),
hash,
path: path.join(paths.repoPath, hash)
}
}

const [
,
user,
Expand Down
1 change: 1 addition & 0 deletions lib/utils/common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports.escapeDots = v => v.replace(/\./g, '\\.')
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sao",
"version": "0.0.0",
"version": "0.0.0-semantic-release",
"description": "Futuristic scaffolding tool ⚔",
"repository": {
"url": "https://github.com/saojs/sao.git",
Expand Down

0 comments on commit 4fc26cc

Please sign in to comment.