diff --git a/package.json b/package.json index b68969d..4edb844 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "dependencies": { "cross-spawn-promise": "^0.10.1", "debug": "^4.1.1", - "electron-installer-common": "^0.4.2", + "electron-installer-common": "^0.5.0", "fs-extra": "^7.0.1", "js-yaml": "^3.10.0", "lodash.filter": "^4.6.0", diff --git a/src/cli.js b/src/cli.js index d156ded..a3326ab 100755 --- a/src/cli.js +++ b/src/cli.js @@ -1,7 +1,7 @@ #!/usr/bin/env node 'use strict' /* -Copyright 2017 Mark Lee and contributors +Copyright 2017, 2018, 2019 Mark Lee and contributors Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -31,13 +31,13 @@ function parseArgs () { describe: 'The absolute path to snapcraft. Defaults to searching in PATH.', string: true }).option('name', { - describe: 'name of the snap package (defaults to name in package.json)', + describe: 'A maximum 30 character name of the snap package (defaults to name in package.json)', string: true }).option('app-version', { describe: 'version of the snap package (defaults to version in package.json)', string: true }).option('summary', { - describe: 'A 78 character long summary for the snap (defaults to description in package.json)', + describe: 'A maximum 78 character long summary for the snap (defaults to description in package.json)', string: true }).option('description', { describe: 'The longer description for the snap', diff --git a/src/index.js b/src/index.js index 51d1701..b3b9989 100644 --- a/src/index.js +++ b/src/index.js @@ -1,6 +1,6 @@ 'use strict' /* -Copyright 2017 Mark Lee and contributors +Copyright 2017, 2018, 2019 Mark Lee and contributors Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -15,6 +15,7 @@ See the License for the specific language governing permissions and limitations under the License. */ +const common = require('electron-installer-common') const debug = require('debug')('electron-installer-snap:index') const fs = require('fs-extra') const nodeify = require('nodeify') @@ -40,6 +41,7 @@ class SnapCreator { setOptions (defaultArgs, userSupplied) { this.config = Object.assign(defaultArgs, userSupplied) + this.config.name = this.sanitizeName(this.config.name) this.snapcraft = new Snapcraft() const snapArch = this.snapcraft.translateArch(String(this.config.arch || process.arch)) @@ -57,6 +59,19 @@ class SnapCreator { return this.snapcraftOptions } + sanitizeName (name) { + if (name.length > 30) { + throw new Error(`The max length of the name is 30 characters, you have ${name.length}`) + } + + const sanitized = common.sanitizeName(name.toLowerCase(), '-a-z0-9') + if (!/[a-z]/.test(sanitized)) { + throw new Error('The snap name needs to have at least one letter') + } + + return sanitized + } + runInTempSnapDir () { return tmp.dir({ prefix: 'electron-snap-', unsafeCleanup: !debug.enabled }) .then(tmpdir => { @@ -101,3 +116,4 @@ function createSnap (userSupplied) { } module.exports = nodeify(createSnap) +module.exports.SnapCreator = SnapCreator diff --git a/test/index.js b/test/index.js index aba4979..94d0fac 100644 --- a/test/index.js +++ b/test/index.js @@ -1,6 +1,6 @@ 'use strict' /* -Copyright 2018 Mark Lee and contributors +Copyright 2018, 2019 Mark Lee and contributors Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -29,6 +29,21 @@ test('packaged app not found', t => t.throwsAsync(snap({}), /Could not find, rea test('cannot find custom snapcraft', t => t.throwsAsync(snap({ src: path.join(__dirname, 'fixtures', 'app-with-asar'), snapcraft: '/foo/bar/non-existent' }), /Cannot locate \/foo\/bar\/non-existent in your system/)) +test('snap name is sanitized', t => { + const creator = new snap.SnapCreator() + t.is(creator.sanitizeName('My App'), 'my-app') +}) + +test('snap name is too long', t => { + const creator = new snap.SnapCreator() + t.throws(() => creator.sanitizeName('My super duper long application name'), /The max length of the name/) +}) + +test('snap name has no letters', t => { + const creator = new snap.SnapCreator() + t.throws(() => creator.sanitizeName('0-9'), /needs to have at least one letter/) +}) + if (!process.env['FAST_TESTS_ONLY']) { test.serial('creates a snap', t => { let snapPath