Skip to content

Commit

Permalink
fix(assets): handle commonPath corner case
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Jul 7, 2023
1 parent cc3821b commit 1e9ac5e
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 8 deletions.
10 changes: 3 additions & 7 deletions src/main/js/gh.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {log} from './log.js'
import {getRepo, pushCommit} from './git.js'
import {formatTag} from './meta.js'
import {formatReleaseNotes} from './changelog.js'
import {asArray, msgJoin} from './util.js'
import {asArray, getCommonPath, msgJoin} from './util.js'

// https://docs.github.com/en/rest/releases/releases?apiVersion=2022-11-28#create-a-release
export const ghRelease = async (pkg) => {
Expand Down Expand Up @@ -74,13 +74,9 @@ export const ghPrepareAssets = async (assets, _cwd) => {
await fs.copy(path.join(cwd, files[0]), target)
return
}
const prefix = getCommonPath(files)

const common = files.length === 1
? files[0].lastIndexOf('/') + 1
: [...(files[0])].findIndex((c, i) => files.some(f => f.charAt(i) !== c))
const prefix = files[0].slice(0, common)

return $.raw`tar -C ${common ? path.join(cwd, prefix) : cwd} -cv${zip ? 'z' : ''}f ${target} ${files.map(f => common ? f.slice(common) : f).join(' ')}`
return $.raw`tar -C ${path.join(cwd, prefix)} -cv${zip ? 'z' : ''}f ${target} ${files.map(f => f.slice(prefix.length)).join(' ')}`
}))

return temp
Expand Down
14 changes: 14 additions & 0 deletions src/main/js/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,17 @@ export const memoizeBy = (fn, getKey = v => v, memo = new Map()) => async (...ar
export const camelize = s => s.replace(/-./g, x => x[1].toUpperCase())

export const asArray = v => Array.isArray(v) ? v : [v]

export const getCommonPath = files => {
const f0 = files[0]
const common = files.length === 1
? f0.lastIndexOf('/') + 1
: [...(f0)].findIndex((c, i) => files.some(f => f.charAt(i) !== c))

const p = f0.slice(0, common)
if (p.endsWith('/')) {
return p
}

return p.slice(0, p.lastIndexOf('/') + 1)
}
48 changes: 47 additions & 1 deletion src/test/js/util.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {suite} from 'uvu'
import * as assert from 'uvu/assert'
import {tpl, get, set} from '../../main/js/util.js'
import {tpl, get, set, getCommonPath} from '../../main/js/util.js'

const test = suite('util')

Expand Down Expand Up @@ -41,5 +41,51 @@ test('set/get()', () => {
assert.equal(get(obj), obj)
})

test('getCommonPath()', () => {
const cases = [
[
'single dir',
[
'foo/bar/'
],
'foo/bar/'
],
[
'single file',
[
'baz.json'
],
''
],
[
'single file in nested dir',
[
'foo/bar/baz.json'
],
'foo/bar/'
],
[
'pair of files in the same dir',
[
'foo/bar.json',
'foo/baz.json'
],
'foo/'
],
[
'pair of dirs',
[
'foo/bar/qux/',
'foo/bar/baz/'
],
'foo/bar/'
]
];

cases.forEach(([name, input, expected]) => {
assert.equal(getCommonPath(input), expected, name)
})
})

test.run()

0 comments on commit 1e9ac5e

Please sign in to comment.