Skip to content

Commit

Permalink
fix: apply dir strip to tar inners
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Jun 27, 2023
1 parent 8b07b9e commit 3f5ccd8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
14 changes: 9 additions & 5 deletions src/main/js/gh.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,24 @@ export const ghPages = queuefy(async (pkg) => {
export const ghPrepareAssets = async (assets, _cwd) => {
const temp = tempy.temporaryDirectory()

await Promise.all(assets.map(async ({name, source = 'target/**/*', zip, cwd = _cwd}) => {
await Promise.all(assets.map(async ({name, source = 'target/**/*', zip, cwd = _cwd, strip = true}) => {
const patterns = asArray(source)
const target = path.join(temp, name)
if (patterns.some(s => s.includes('*'))) {
zip = true
}
const files = await glob(patterns, {cwd, absolute: true, onlyFiles: true})

const files = await glob(patterns, {cwd, absolute: false, onlyFiles: true})
if (!zip && files.length === 1) {
await fs.copy(files[0], target)
await fs.copy(path.join(cwd, files[0]), target)
return
}

return $.raw`tar -C ${cwd} -cv${zip ? 'z' : ''}f ${target} ${files.join(' ')}`
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 temp
Expand Down
15 changes: 10 additions & 5 deletions src/test/js/gh.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,21 @@ const test = suite('gh')

test('`prepareAssets()` preprocesses files for publishing', async () => {
const cwd = tempy.temporaryDirectory()
await fs.outputFile(path.resolve(cwd, 'a.json'), '{"foo": "bar"}')
await fs.outputFile(path.resolve(cwd, 'b.json'), '{"baz": "qux"}')
await fs.outputFile(path.resolve(cwd, 'target/json/a.json'), '{"foo": "bar"}')
await fs.outputFile(path.resolve(cwd, 'target/json/b.json'), '{"baz": "qux"}')
await fs.outputFile(path.resolve(cwd, 'c.json'), '{"quuux": "quuux"}')
await fs.outputFile(path.resolve(cwd, 'd.json'), '{"duuux": "duuux"}')

const temp = await ghPrepareAssets([
{ name: 'jsons.zip', source: ['*.json'], zip: true, cwd},
{ name: 'a.zip', source: 'a.json', zip: true },
{ name: 'a.json', source: 'a.json', cwd },
{ name: 'jsons.zip', source: ['target/**/*.json'], zip: true, cwd},
{ name: 'a.zip', source: 'target/json/a.json', zip: true },
{ name: 'a.json', source: 'target/json/a.json', cwd },
{ name: 'c.json', source: 'c.json', cwd },
{ name: 'jsons2.zip', source: '*.json', cwd },
], cwd)

assert.equal(await fs.readFile(path.resolve(temp, 'a.json'), 'utf8'), '{"foo": "bar"}')
assert.equal(await fs.readFile(path.resolve(temp, 'c.json'), 'utf8'), '{"quuux": "quuux"}')
})

test.run()

0 comments on commit 3f5ccd8

Please sign in to comment.