diff --git a/classes/alias.js b/classes/alias.js index dc22e385..2ffa2847 100644 --- a/classes/alias.js +++ b/classes/alias.js @@ -59,7 +59,9 @@ module.exports = class Alias { data.org = message.org || ''; data.integrity = message.integrity || ''; - data.version = message.version || this.version; + // We'll use message.version when the cache has been properly purged + // data.version = message.version || this.version; + data.version = this.version; data.name = message.name || this.name; data.files = message.files || []; @@ -68,10 +70,6 @@ module.exports = class Alias { let status = err.statusCode; if (status === 409) { - this.log.debug( - 'Alias already exists on server, performing update', - ); - try { const { message: msg } = await request({ host: this.server, @@ -83,7 +81,9 @@ module.exports = class Alias { data.org = msg.org || ''; data.integrity = msg.integrity || ''; - data.version = msg.version || this.version; + // We'll use msg.version when the cache has been properly purged + // data.version = msg.version || this.version; + data.version = this.version; data.name = msg.name || this.name; data.files = msg.files || []; data.update = true; diff --git a/commands/alias.js b/commands/alias.js new file mode 100644 index 00000000..42c026c0 --- /dev/null +++ b/commands/alias.js @@ -0,0 +1,125 @@ +/* eslint-disable no-nested-ternary */ +const ora = require('ora'); +const { configStore, getDefaults } = require('@eik/common-config-loader'); +const { getCWD, logger } = require('../utils'); + +const Alias = require('../classes/alias'); +const { Alias: AliasFormatter } = require('../formatters'); + +exports.command = 'alias '; + +exports.aliases = [ + 'ma', + 'pa', + 'pkg-alias', + 'na', + 'dep-alias', + 'dependency-alias', +]; + +exports.describe = `Create a semver major alias for a package or an import map as identified by its name and version. + A package or import map with the given name and version must already exist on the Eik server + Alias should be the semver major part of the version. + Eg. For a package or import map of version 5.4.3, you should use 5 as the alias`; + +exports.builder = (yargs) => { + const cwd = getCWD(); + const defaults = getDefaults(cwd); + + yargs + .positional('name', { + describe: `Name of package or map that is to be aliased`, + type: 'string', + }) + .positional('version', { + describe: `Version for package or map that is to be aliased`, + type: 'string', + }) + .positional('alias', { + describe: `Alias for a semver version. Should be the semver major component of version.`, + type: 'string', + }); + + yargs.options({ + server: { + alias: 's', + describe: 'Specify location of Eik server.', + default: defaults.server, + }, + cwd: { + alias: 'c', + describe: 'Alter current working directory.', + default: defaults.cwd, + }, + debug: { + describe: 'Log additional messages', + default: false, + type: 'boolean', + }, + token: { + describe: + 'Provide a jwt token to be used to authenticate with the Eik server.', + default: '', + alias: 't', + }, + }); + + yargs.default('token', defaults.token, defaults.token ? '######' : ''); + + yargs.example(`eik alias my-app 1.6.2 1`); + yargs.example(`eik alias my-map 1.0.1 1`); + yargs.example(`eik alias lodash 4.17.19 4`); + yargs.example( + `eik alias my-map 6.3.1 6 --server https://assets.myeikserver.com`, + ); + yargs.example(`eik alias my-map 4.2.2 4 --debug`); +}; + +exports.handler = async (argv) => { + const spinner = ora({ stream: process.stdout }).start('working...'); + let success = false; + const { cwd, type: typeFromArgs, debug } = argv; + const config = configStore.findInDirectory(cwd); + const { server, type } = config; + + const typeOverride = typeFromArgs || type; + const log = logger(spinner, debug); + + const isPackage = ['package', 'npm'].includes(typeOverride); + + try { + const data = await new Alias({ + type: + typeOverride === 'package' + ? 'pkg' + : typeOverride === 'npm' + ? 'npm' + : typeOverride === 'map' + ? 'map' + : 'pkg', + logger: log, + ...argv, + }).run(); + + const createdOrUpdated = data.update ? 'Updated' : 'Created'; + log.info( + `${createdOrUpdated} alias for ${isPackage ? 'package' : 'map'} "${ + data.name + }". ("${data.version}" => "v${data.alias}")`, + ); + success = true; + + spinner.text = ''; + spinner.stopAndPersist(); + if (success) { + new AliasFormatter(data).format(server); + } else { + process.exit(1); + } + } catch (err) { + spinner.warn(err.message); + spinner.text = ''; + spinner.stopAndPersist(); + process.exit(1); + } +}; diff --git a/commands/map-alias.js b/commands/map-alias.js deleted file mode 100644 index 2f43c5d8..00000000 --- a/commands/map-alias.js +++ /dev/null @@ -1,105 +0,0 @@ -'use strict'; - -const ora = require('ora'); -const { getDefaults } = require('@eik/common-config-loader'); -const Alias = require('../classes/alias'); -const { logger, getCWD } = require('../utils'); -const { Alias: AliasFormatter } = require('../formatters'); - -exports.command = 'map-alias '; - -exports.aliases = ['ma']; - -exports.describe = `Create a semver major alias for an import map as identified by its name and version. - An import map with the given name and version must already exist on asset server - Alias should be the semver major part of the import map version. - Eg. For an import map of version 5.4.3, you should use 5 as the alias`; - -exports.builder = (yargs) => { - const cwd = getCWD(); - const defaults = getDefaults(cwd); - - yargs - .positional('name', { - describe: `Import map name for import map that is to be aliased`, - type: 'string', - }) - .positional('version', { - describe: `Import map version for import map that is to be aliased`, - type: 'string', - }) - .positional('alias', { - describe: `Alias for a semver version. Should be the semver major component of version.`, - type: 'string', - }); - - yargs.options({ - server: { - alias: 's', - describe: 'Specify location of asset server.', - default: defaults.server, - }, - cwd: { - alias: 'c', - describe: 'Alter current working directory.', - default: defaults.cwd, - }, - debug: { - describe: 'Logs additional messages', - default: false, - type: 'boolean', - }, - token: { - describe: - 'Provide a jwt token to be used to authenticate with the Eik server.', - default: '', - alias: 't', - }, - }); - - yargs.default('token', defaults.token, defaults.token ? '######' : ''); - - yargs.example(`eik map-alias my-map 1.0.0 1`); - yargs.example(`eik map-alias my-map 1.7.3 1`); - yargs.example(`eik map-alias my-map 6.3.1 6`); - yargs.example( - `eik map-alias my-map 6.3.1 6 --server https://assets.myeikserver.com`, - ); - yargs.example(`eik map-alias my-map 4.2.2 4 --debug`); -}; - -exports.handler = async (argv) => { - const spinner = ora({ stream: process.stdout }).start('working...'); - let success = false; - const { debug, name, version, server } = argv; - const log = logger(spinner, debug); - let data = {}; - - try { - data = await new Alias({ - type: 'map', - logger: log, - ...argv, - }).run(); - - data.name = name; - data.version = version; - data.files = []; - - const createdOrUpdated = data.update ? 'Updated' : 'Created'; - log.info( - `${createdOrUpdated} alias for package "${data.name}". ("${data.version}" => "v${data.alias}")`, - ); - success = true; - } catch (err) { - log.warn(err.message); - } - - spinner.text = ''; - spinner.stopAndPersist(); - if (success) { - new AliasFormatter(data).format(server); - } else { - process.exit(1); - } -}; diff --git a/commands/npm-alias.js b/commands/npm-alias.js deleted file mode 100644 index d35d0ae9..00000000 --- a/commands/npm-alias.js +++ /dev/null @@ -1,100 +0,0 @@ -'use strict'; - -const ora = require('ora'); -const { getDefaults } = require('@eik/common-config-loader'); -const Alias = require('../classes/alias'); -const { logger, getCWD } = require('../utils'); -const { Alias: AliasFormatter } = require('../formatters'); - -exports.command = 'npm-alias '; - -exports.aliases = ['na', 'dep-alias', 'dependency-alias']; - -exports.describe = `Create a semver major alias for an NPM package as identified by its name and version. - An NPM package with the given name and version must already exist on the asset server - Alias should be the semver major part of the NPM package version. - Eg. For an NPM package of version 5.4.3, you should use 5 as the alias`; - -exports.builder = (yargs) => { - const cwd = getCWD(); - const defaults = getDefaults(cwd); - - yargs - .positional('name', { - describe: 'Name matching NPM package name.', - type: 'string', - }) - .positional('version', { - describe: 'Version matching NPM package version.', - type: 'string', - }) - .positional('alias', { - describe: - 'Alias for a semver version. Must be the semver major component of version.', - type: 'string', - }); - - yargs.options({ - server: { - alias: 's', - describe: 'Specify location of asset server.', - default: defaults.server, - }, - cwd: { - alias: 'c', - describe: 'Alter current working directory.', - default: defaults.cwd, - }, - debug: { - describe: 'Logs additional messages', - default: false, - type: 'boolean', - }, - token: { - describe: - 'Provide a jwt token to be used to authenticate with the Eik server.', - default: '', - alias: 't', - }, - }); - - yargs.default('token', defaults.token, defaults.token ? '######' : ''); - - yargs.example(`eik npm lit-html 1.0.0 1`); - yargs.example(`eik npm lit-html 1.3.5 1 --debug`); - yargs.example( - `eik npm lit-html 5.3.2 5 --server https://assets.myeikserver.com`, - ); -}; - -exports.handler = async (argv) => { - const spinner = ora({ stream: process.stdout }).start('working...'); - let success = false; - const { debug, server } = argv; - const log = logger(spinner, debug); - let data = {}; - - try { - data = await new Alias({ - type: 'npm', - logger: log, - ...argv, - }).run(); - - const createdOrUpdated = data.update ? 'Updated' : 'Created'; - log.info( - `${createdOrUpdated} alias for package "${data.name}". ("${data.version}" => "v${data.alias}")`, - ); - success = true; - } catch (err) { - log.warn(err.message); - } - - spinner.text = ''; - spinner.stopAndPersist(); - if (success) { - new AliasFormatter(data).format(server); - } else { - process.exit(1); - } -}; diff --git a/commands/package-alias.js b/commands/package-alias.js deleted file mode 100644 index 0a9b99ee..00000000 --- a/commands/package-alias.js +++ /dev/null @@ -1,109 +0,0 @@ -'use strict'; - -const ora = require('ora'); -const semver = require('semver'); -const { getDefaults } = require('@eik/common-config-loader'); -const Alias = require('../classes/alias'); -const { logger, getCWD } = require('../utils'); -const { Alias: AliasFormatter } = require('../formatters'); - -exports.command = 'package-alias [name] [version] [alias]'; - -exports.aliases = ['pkg-alias', 'pa']; - -exports.describe = `Create a semver major alias for a package as identified by its name and version. - A package with the given name and version must already exist on asset server - Alias should be the semver major part of the package version. - Eg. For a package of version 5.4.3, you should use 5 as the alias`; - -exports.builder = (yargs) => { - const cwd = getCWD(); - const defaults = getDefaults(cwd); - - yargs - .positional('name', { - describe: 'Name matching existing name for a package on Eik server', - type: 'string', - default: defaults.name, - }) - .positional('version', { - describe: - 'Version matching existing version for a package on Eik server', - type: 'string', - default: defaults.version, - }) - .positional('alias', { - describe: - 'Alias for a semver version. Must be the semver major component of version. Eg. 1.0.0 should be given as 1', - type: 'string', - default: defaults.version ? semver.major(defaults.version) : null, - }); - - yargs.options({ - server: { - alias: 's', - describe: 'Specify location of Eik asset server.', - default: defaults.server, - }, - cwd: { - alias: 'c', - describe: 'Alter the current working directory.', - default: defaults.cwd, - }, - debug: { - describe: 'Logs additional messages', - default: false, - type: 'boolean', - }, - token: { - describe: - 'Provide a jwt token to be used to authenticate with the Eik server.', - default: '', - alias: 't', - }, - }); - - yargs.default('token', defaults.token, defaults.token ? '######' : ''); - - yargs.example(`eik package-alias my-app 1.0.0 1`); - yargs.example(`eik package-alias my-app 1.7.3 1`); - yargs.example(`eik package-alias my-app 6.3.1 6`); - yargs.example( - `eik package-alias my-app 6.3.1 6 --server https://assets.myeikserver.com`, - ); - yargs.example(`eik package-alias my-app 4.2.2 4 --debug`); -}; - -exports.handler = async (argv) => { - const spinner = ora({ stream: process.stdout }).start('working...'); - let success = false; - const { debug, server } = argv; - const log = logger(spinner, debug); - let af; - - try { - const data = await new Alias({ - type: 'pkg', - logger: log, - ...argv, - }).run(); - - af = new AliasFormatter(data); - - const createdOrUpdated = data.update ? 'Updated' : 'Created'; - log.info( - `${createdOrUpdated} alias for package "${data.name}". ("${data.version}" => "v${data.alias}")`, - ); - success = true; - } catch (err) { - log.warn(err.message); - } - - spinner.text = ''; - spinner.stopAndPersist(); - if (success) { - af.format(server); - } else { - process.exit(1); - } -}; diff --git a/formatters/alias.js b/formatters/alias.js index bd5a7fa7..d65f0182 100644 --- a/formatters/alias.js +++ b/formatters/alias.js @@ -55,6 +55,12 @@ class Alias { if (this.update) { write(`${chalk.bgMagenta.white(' UPDATED \n\n')}`); + + write( + `${chalk.yellow.bold( + '! It may take up to 40 minutes before this alias updates due to caching.', + )}\n\n`, + ); } else { write(`${chalk.bgGreen.white(' NEW ')}\n\n`); } diff --git a/test/alias.test.js b/test/alias.test.js index 76393bed..25a30ece 100644 --- a/test/alias.test.js +++ b/test/alias.test.js @@ -41,6 +41,7 @@ test('Creating a package alias', async (t) => { await cli.publish({ server: address, + type: 'package', name: 'my-pack', version: '1.0.0', token, @@ -86,12 +87,12 @@ test('Creating an npm alias', async (t) => { await cli.publish({ server: address, + type: 'npm', name: 'lit-html', version: '1.1.2', debug: true, token, cwd, - type: 'npm', files: { 'index.js': join(__dirname, './fixtures/client.js'), }, @@ -134,6 +135,7 @@ test('Creating a map alias', async (t) => { await cli.map({ server: address, + type: 'map', name: 'my-map', version: '1.0.0', file: join(__dirname, 'fixtures/import-map.json'), diff --git a/test/integration/alias.test.js b/test/integration/alias.test.js index 81860597..15c66c7f 100644 --- a/test/integration/alias.test.js +++ b/test/integration/alias.test.js @@ -40,8 +40,8 @@ beforeEach(async (t) => { const assets = { name: 'scroll-into-view-if-needed', - version: '2.2.24', type: 'npm', + version: '2.2.24', server: address, files: { 'index.js': join(__dirname, './../fixtures/client.js'), @@ -51,7 +51,7 @@ beforeEach(async (t) => { await fs.writeFile(join(folder, 'eik.json'), JSON.stringify(assets)); - const cmd = `${eik} package --token ${token} --cwd ${folder}`; + const cmd = `${eik} publish --token ${token} --cwd ${folder}`; await exec(cmd); // Write map into own folder @@ -93,12 +93,13 @@ afterEach(async (t) => { await t.context.server.close(); }); -test('eik package-alias ', async (t) => { +test('eik alias ', async (t) => { const { address, token, folder: cwd } = t.context; const eik = join(__dirname, '../../index.js'); const assets = { server: address, + type: 'package', name: 'my-pack', version: '1.0.0', files: { @@ -109,13 +110,10 @@ test('eik package-alias ', async (t) => { await fs.writeFile(join(cwd, 'eik.json'), JSON.stringify(assets)); - const cmd1 = `${eik} package --token ${token} --cwd ${cwd}`; + const cmd1 = `${eik} publish --token ${token} --cwd ${cwd}`; await exec(cmd1); - const cmd2 = `${eik} package-alias my-pack 1.0.0 1 - --token ${token} - --server ${address} - --cwd ${cwd}`; + const cmd2 = `${eik} alias my-pack 1.0.0 1 --token ${token} --server ${address} --cwd ${cwd}`; const { error, stdout } = await exec(cmd2.split('\n').join(' ')); @@ -130,12 +128,9 @@ test('eik package-alias ', async (t) => { t.match(stdout, 'NEW'); }); -test('eik npm-alias --token --server : no eik.json or .eikrc', async (t) => { +test('eik alias --token --server : no eik.json or .eikrc', async (t) => { const eik = join(__dirname, '../../index.js'); - const cmd = `${eik} npm-alias scroll-into-view-if-needed 2.2.24 2 - --token ${t.context.token} - --server ${t.context.address} - --cwd ${t.context.folder}`; + const cmd = `${eik} alias scroll-into-view-if-needed 2.2.24 2 --token ${t.context.token} --server ${t.context.address} --cwd ${t.context.folder}`; const { error, stdout } = await exec(cmd.split('\n').join(' ')); @@ -156,9 +151,10 @@ test('eik npm-alias --token --server : no eik.json or . t.end(); }); -test('eik npm-alias : publish details provided by eik.json file', async (t) => { +test('eik alias : publish details provided by eik.json file', async (t) => { const assets = { name: 'test-app', + type: 'npm', version: '1.0.0', server: t.context.address, files: { @@ -171,7 +167,7 @@ test('eik npm-alias : publish details provided by eik.j JSON.stringify(assets), ); const eik = join(__dirname, '../../index.js'); - const cmd = `${eik} npm-alias scroll-into-view-if-needed 2.2.24 2 --token ${t.context.token} --cwd ${t.context.folder}`; + const cmd = `${eik} alias scroll-into-view-if-needed 2.2.24 2 --token ${t.context.token} --cwd ${t.context.folder}`; const { error, stdout } = await exec(cmd); @@ -192,9 +188,9 @@ test('eik npm-alias : publish details provided by eik.j t.end(); }); -test('eik map-alias --token --server : no eik.json or .eikrc', async (t) => { +test('eik alias --token --server : no eik.json or .eikrc', async (t) => { const eik = join(__dirname, '../../index.js'); - const cmd = `${eik} map-alias test-map 1.0.0 1 --token ${t.context.token} --server ${t.context.address} --cwd ${t.context.folder}/map`; + const cmd = `${eik} alias test-map 1.0.0 1 --token ${t.context.token} --server ${t.context.address} --cwd ${t.context.folder}/map`; const { error, stdout } = await exec(cmd.split('\n').join(' ')); @@ -211,9 +207,10 @@ test('eik map-alias --token --server : no eik.json or . t.end(); }); -test('eik map-alias : publish details provided by eik.json file', async (t) => { +test('eik alias : publish details provided by eik.json file', async (t) => { const assets = { name: 'test-app', + type: 'map', version: '1.0.0', server: t.context.address, files: { @@ -227,7 +224,7 @@ test('eik map-alias : publish details provided by eik.j ); const eik = join(__dirname, '../../index.js'); - const cmd = `${eik} map-alias test-map 1.0.0 1 --server ${t.context.address} --token ${t.context.token} --cwd ${t.context.folder}/map`; + const cmd = `${eik} alias test-map 1.0.0 1 --server ${t.context.address} --token ${t.context.token} --cwd ${t.context.folder}/map`; const { error, stdout } = await exec(cmd); diff --git a/test/integration/package.test.js b/test/integration/publish.test.js similarity index 89% rename from test/integration/package.test.js rename to test/integration/publish.test.js index 36c7d72d..343c0ef6 100644 --- a/test/integration/package.test.js +++ b/test/integration/publish.test.js @@ -11,7 +11,7 @@ const { test, beforeEach, afterEach } = require('tap'); const fetch = require('node-fetch'); const EikService = require('@eik/service'); const { sink } = require('@eik/core'); -const cli = require('../..'); +const cli = require('../../classes'); function exec(cmd) { return new Promise((resolve) => { @@ -44,7 +44,7 @@ afterEach(async (t) => { await t.context.server.close(); }); -test('eik package : package, details provided by eik.json file', async (t) => { +test('eik publish : package, details provided by eik.json file', async (t) => { const assets = { name: 'test-app', version: '1.0.0', @@ -61,7 +61,7 @@ test('eik package : package, details provided by eik.json file', async (t) => { ); const eik = join(__dirname, '../../index.js'); - const cmd = `${eik} package --token ${t.context.token} --cwd ${t.context.folder}`; + const cmd = `${eik} publish --token ${t.context.token} --cwd ${t.context.folder}`; const { error, stdout } = await exec(cmd); @@ -77,11 +77,11 @@ test('eik package : package, details provided by eik.json file', async (t) => { t.end(); }); -test('eik package : package, details provided by eik.json file - npm namespace', async (t) => { +test('eik publish : npm, details provided by eik.json file - npm namespace', async (t) => { const assets = { name: 'test-app', - version: '1.0.0', type: 'npm', + version: '1.0.0', server: t.context.address, files: { 'index.js': join(__dirname, './../fixtures/client.js'), @@ -95,7 +95,7 @@ test('eik package : package, details provided by eik.json file - npm namespace', ); const eik = join(__dirname, '../../index.js'); - const cmd = `${eik} package --token ${t.context.token} --cwd ${t.context.folder} --npm`; + const cmd = `${eik} publish --token ${t.context.token} --cwd ${t.context.folder} --npm`; const { error, stdout } = await exec(cmd); @@ -111,7 +111,7 @@ test('eik package : package, details provided by eik.json file - npm namespace', t.end(); }); -test('eik package : package, details provided by eik.json file - explicit package namespace', async (t) => { +test('eik publish : package, details provided by eik.json file - explicit package namespace', async (t) => { const assets = { name: 'test-app', version: '1.0.0', @@ -129,7 +129,7 @@ test('eik package : package, details provided by eik.json file - explicit packag ); const eik = join(__dirname, '../../index.js'); - const cmd = `${eik} package --token ${t.context.token} --cwd ${t.context.folder} --npm`; + const cmd = `${eik} publish --token ${t.context.token} --cwd ${t.context.folder} --npm`; const { error, stdout } = await exec(cmd); @@ -145,7 +145,7 @@ test('eik package : package, details provided by eik.json file - explicit packag t.end(); }); -test('eik package : package, details provided by package.json values', async (t) => { +test('eik publish : package, details provided by package.json values', async (t) => { const assets = { name: 'test-app', version: '1.0.0', @@ -164,7 +164,7 @@ test('eik package : package, details provided by package.json values', async (t) ); const eik = join(__dirname, '../../index.js'); - const cmd = `${eik} package --token ${t.context.token} --cwd ${t.context.folder}`; + const cmd = `${eik} publish --token ${t.context.token} --cwd ${t.context.folder}`; const { error, stdout } = await exec(cmd); @@ -180,7 +180,7 @@ test('eik package : package, details provided by package.json values', async (t) t.end(); }); -test('eik package : package, details provided by package.json values and eik.json, throws error', async (t) => { +test('eik publish : package, details provided by package.json values and eik.json, throws error', async (t) => { const pkg = { name: 'test-app', version: '1.0.0', @@ -214,7 +214,7 @@ test('eik package : package, details provided by package.json values and eik.jso ); const eik = join(__dirname, '../../index.js'); - const cmd = `${eik} package --token ${t.context.token} --cwd ${t.context.folder}`; + const cmd = `${eik} publish --token ${t.context.token} --cwd ${t.context.folder}`; const { error } = await exec(cmd); @@ -233,6 +233,7 @@ test('workflow: publish npm, alias npm, publish map, alias map and then publish // publish npm dep let assets = { name: 'scroll-into-view-if-needed', + type: 'npm', version: '2.2.24', server: t.context.address, files: { @@ -250,9 +251,7 @@ test('workflow: publish npm, alias npm, publish map, alias map and then publish await exec(cmd); // alias npm dependency - cmd = `${eik} npm-alias scroll-into-view-if-needed 2.2.24 2 - --token ${t.context.token} - --server ${t.context.address}`; + cmd = `${eik} alias scroll-into-view-if-needed 2.2.24 2 --token ${t.context.token} --server ${t.context.address}`; await exec(cmd.split('\n').join(' ')); // create import map file locally @@ -274,7 +273,7 @@ test('workflow: publish npm, alias npm, publish map, alias map and then publish await exec(cmd.split('\n').join(' ')); // alias import map - cmd = `${eik} map-alias my-map 1.0.0 1 --token ${t.context.token} --server ${t.context.address}`; + cmd = `${eik} alias my-map 1.0.0 1 --token ${t.context.token} --server ${t.context.address}`; await exec(cmd.split('\n').join(' ')); assets = {