Skip to content

Commit

Permalink
Sanitize Snap name so it conforms to format requirements (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
malept authored Jan 5, 2019
1 parent a8e399d commit b92d42c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 3 additions & 3 deletions src/cli.js
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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',
Expand Down
18 changes: 17 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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')
Expand All @@ -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))
Expand All @@ -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 => {
Expand Down Expand Up @@ -101,3 +116,4 @@ function createSnap (userSupplied) {
}

module.exports = nodeify(createSnap)
module.exports.SnapCreator = SnapCreator
17 changes: 16 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b92d42c

Please sign in to comment.