diff --git a/bin/cli.js b/bin/cli.js index 357c361..72a094a 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -1,5 +1,6 @@ #!/usr/bin/env node const cac = require('cac').default +const SAOError = require('../lib/SAOError') const cli = cac() @@ -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 `) + } + + 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) }) diff --git a/lib/logger.js b/lib/logger.js index dfa73a8..4a983a0 100644 --- a/lib/logger.js +++ b/lib/logger.js @@ -5,7 +5,7 @@ class Logger { constructor(options) { this.options = Object.assign( { - logLevel: 2 + logLevel: 3 }, options ) diff --git a/lib/parseGenerator.js b/lib/parseGenerator.js index 0d3fc3c..5398495 100644 --- a/lib/parseGenerator.js +++ b/lib/parseGenerator.js @@ -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') /** * @@ -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)) { @@ -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, diff --git a/lib/utils/common.js b/lib/utils/common.js new file mode 100644 index 0000000..54d9dfb --- /dev/null +++ b/lib/utils/common.js @@ -0,0 +1 @@ +exports.escapeDots = v => v.replace(/\./g, '\\.') diff --git a/package.json b/package.json index 7ac0be5..69cf43b 100644 --- a/package.json +++ b/package.json @@ -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",