From add7bcfbea587af68e88e6096f93f572beebdfcd Mon Sep 17 00:00:00 2001 From: Leo Lamprecht Date: Sat, 28 Jan 2017 12:34:17 +0100 Subject: [PATCH] Require `--alias` for all existing kinds of automatic aliasing --- bin/now-alias.js | 29 --------------------------- bin/now-deploy.js | 51 +++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 45 insertions(+), 35 deletions(-) diff --git a/bin/now-alias.js b/bin/now-alias.js index 35e829d78c4..d70cf28a471 100755 --- a/bin/now-alias.js +++ b/bin/now-alias.js @@ -13,7 +13,6 @@ const login = require('../lib/login') const cfg = require('../lib/cfg') const {error} = require('../lib/error') const toHost = require('../lib/to-host') -const readMetaData = require('../lib/read-metadata') const argv = minimist(process.argv.slice(2), { string: ['config', 'token'], @@ -213,11 +212,6 @@ async function run(token) { break } default: { - if (argv._.length === 0) { - await realias(alias) - break - } - if (argv._.length === 2) { await alias.set(String(argv._[0]), String(argv._[1])) } else if (argv._.length >= 3) { @@ -293,26 +287,3 @@ function findAlias(alias, list) { return _alias } - -async function realias(alias) { - const path = process.cwd() - const {nowConfig, name} = await readMetaData(path, { - deploymentType: 'npm', // hard coding settingsā€¦ - quiet: true // `quiet` - }) - - const targets = nowConfig && nowConfig.aliases - - // the user never intended to support aliases from the package - if (!targets || !Array.isArray(targets)) { - help() - return exit(0) - } - - // now try to find the last deployment - const source = await alias.last(name) - - for (const target of targets) { - await alias.set(source.url, target) - } -} diff --git a/bin/now-deploy.js b/bin/now-deploy.js index 82fb35ad4ae..4f4eea2597b 100755 --- a/bin/now-deploy.js +++ b/bin/now-deploy.js @@ -1,7 +1,7 @@ #!/usr/bin/env node // Native -const {resolve} = require('path') +const {resolve, join} = require('path') // Packages const Progress = require('progress') @@ -98,7 +98,7 @@ const help = () => { -e, --env Include an env var (e.g.: ${chalk.dim('`-e KEY=value`')}). Can appear many times. -C, --no-clipboard Do not attempt to copy URL to clipboard -N, --forward-npm Forward login information to install private npm modules - -a, --alias Reassign an existing alias to the deployment + -a, --alias Re-assign existing aliases to the deployment ${chalk.dim('Enforcable Types (when both package.json and Dockerfile exist):')} @@ -168,7 +168,7 @@ const deploymentName = argv.name || false const apiUrl = argv.url || 'https://api.zeit.co' const isTTY = process.stdout.isTTY const quiet = !isTTY -const autoAliases = argv.alias ? flatten([argv.alias]) : [] +const autoAliases = typeof argv.alias === 'undefined' ? false : flatten([argv.alias]) if (argv.config) { cfg.setConfigFile(argv.config) @@ -263,7 +263,7 @@ async function sync(token) { } } - // Make sure that directory is not too big + // Make sure that directory is deployable await checkPath(path) if (!quiet) { @@ -588,13 +588,52 @@ const assignAlias = async (autoAlias, token, deployment) => { await aliases.set(String(deployment), String(related.alias)) } +async function realias(token, host) { + const path = process.cwd() + + const configFiles = { + pkg: join(path, 'package.json'), + nowJSON: join(path, 'now.json') + } + + if (!fs.existsSync(configFiles.pkg) && !fs.existsSync(configFiles.nowJSON)) { + error(`Couldn't find a now.json or package.json file with an alias list in it`) + return + } + + const {nowConfig} = await readMetaData(path, { + deploymentType: 'npm', // hard coding settingsā€¦ + quiet: true // `quiet` + }) + + const targets = nowConfig && nowConfig.aliases + + // the user never intended to support aliases from the package + if (!targets || !Array.isArray(targets)) { + help() + return exit(0) + } + + for (const target of targets) { + await assignAlias(target, token, host) + } +} + function printLogs(host, token) { // log build const logger = new Logger(host, {debug, quiet}) logger.on('close', async () => { - for (const autoAlias of autoAliases) { - await assignAlias(autoAlias, token, host) + if (Array.isArray(autoAliases)) { + const aliasList = autoAliases.filter(item => item !== '') + + if (aliasList.length > 0) { + for (const alias of aliasList) { + await assignAlias(alias, token, host) + } + } else { + await realias(token, host) + } } if (!quiet) {