-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
194 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
'use strict'; | ||
|
||
/* eslint-disable no-await-in-loop */ | ||
|
||
module.exports = clean; | ||
module.exports.NAME = 'clean'; | ||
module.exports.filterExceptions = filterExceptions; | ||
module.exports.extractKeysFromUpdatesJson = extractKeysFromUpdatesJson; | ||
|
||
/** | ||
* Return promise | ||
* @param {object} options | ||
* @return {Promise} | ||
*/ | ||
async function clean(options) { | ||
/** @type {AbstractTransport} */ | ||
const transport = options.transport.instance; | ||
const updatesJson = await transport.fetchUpdatesJson(); | ||
|
||
if (updatesJson.isEmpty) { | ||
console.warn('Can\'t clean because updates.json is not available.'); | ||
return []; | ||
} | ||
|
||
const list = filterExceptions(await transport.fetchBuildsList(), [ | ||
...extractKeysFromUpdatesJson(updatesJson), | ||
...(options.except && options.except.forEach ? options.except : []), | ||
]); | ||
|
||
for (const build of list) { | ||
await transport.beforeRemove(build); | ||
await transport.removeBuild(build); | ||
await transport.afterRemove(build); | ||
} | ||
|
||
return list; | ||
} | ||
|
||
function filterExceptions(list, exceptions) { | ||
return list.filter((build) => { | ||
return !exceptions.some(ex => build.includes(ex)); | ||
}); | ||
} | ||
|
||
function extractKeysFromUpdatesJson(updatesJson) { | ||
return Object.values(updatesJson).reduce((keys, section) => { | ||
if (!section) return keys; | ||
|
||
Object.values(section).forEach((str) => { | ||
if (!str.match) return; | ||
|
||
const matches = str.match(/(\w+-v\d+\.\d+\.\d+)/); | ||
if (matches && matches[1]) { | ||
keys.push(matches[1]); | ||
} | ||
}); | ||
|
||
return keys; | ||
}, []); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
'use strict'; | ||
|
||
const { expect } = require('chai'); | ||
const { | ||
extractKeysFromUpdatesJson, | ||
filterExceptions, | ||
} = require('./clean'); | ||
|
||
describe('Clean command', () => { | ||
it('filterExceptions should return values not existed in preserved', () => { | ||
const values = ['win32-v0.0.1', 'win32-x64-stage-0.1.0', 'test0.0.1']; | ||
|
||
expect(filterExceptions(values, ['win32'])).to.deep.equal([ | ||
'test0.0.1', | ||
]); | ||
|
||
expect(filterExceptions(values, ['0.0.1'])).to.deep.equal([ | ||
'win32-x64-stage-0.1.0', | ||
]); | ||
}); | ||
|
||
it('extractKeysFromUpdatesJson should extract strings like buildId', () => { | ||
const json = { | ||
win32: { | ||
install: 'https://example.com/win32-v0.0.1', | ||
}, | ||
'linux-x64-stage': { | ||
prevVersion: 'v1.0.2-beta', | ||
comment: '1.0.2', | ||
install: 'https://example.com/linux-x64-stage-v1.0.2', | ||
}, | ||
}; | ||
|
||
expect(extractKeysFromUpdatesJson(json)).to.deep.equal([ | ||
'win32-v0.0.1', | ||
'stage-v1.0.2', | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters