0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-04-07 23:01:25 -05:00

refactor(cli): update version command and add unit tests

This commit is contained in:
Gao Sun 2022-10-12 22:01:17 +08:00
parent 926e69fd69
commit a19a522894
No known key found for this signature in database
GPG key ID: 13EBE123E4773688
4 changed files with 69 additions and 11 deletions

View file

@ -12,13 +12,17 @@ const pool = createMockPool({
},
});
const files = Object.freeze([
{ filename: '1.0.0-1663923770-a.js', path: '/alterations/1.0.0-1663923770-a.js' },
{ filename: '1.0.0-1663923771-b.js', path: '/alterations/1.0.0-1663923771-b.js' },
{ filename: '1.0.0-1663923772-c.js', path: '/alterations/1.0.0-1663923772-c.js' },
]);
const mockExit = jest.fn((code?: number) => {
throw new Error(String(code));
});
describe('getUndeployedAlterations()', () => {
const files = Object.freeze([
{ filename: '1.0.0-1663923770-a.js', path: '/alterations/1.0.0-1663923770-a.js' },
{ filename: '1.0.0-1663923771-b.js', path: '/alterations/1.0.0-1663923771-b.js' },
{ filename: '1.0.0-1663923772-c.js', path: '/alterations/1.0.0-1663923772-c.js' },
]);
beforeEach(() => {
// `getAlterationFiles()` will ensure the order
jest.spyOn(functions, 'getAlterationFiles').mockResolvedValueOnce([...files]);
@ -38,3 +42,40 @@ describe('getUndeployedAlterations()', () => {
await expect(functions.getUndeployedAlterations(pool)).resolves.toEqual([files[1], files[2]]);
});
});
describe('chooseAlterationsByVersion()', () => {
const files = Object.freeze(
[
'1.0.0_beta.9-1663923770-a.js',
'1.0.0_beta.9-1663923771-b.js',
'1.0.0_beta.10-1663923772-c.js',
'1.0.0_beta.11-1663923773-c.js',
'1.0.0_beta.11-1663923774-c.js',
'1.0.0-1663923775-c.js',
'1.0.0-1663923776-c.js',
'1.0.1-1663923777-c.js',
'1.2.0-1663923778-c.js',
].map((filename) => ({ filename, path: '/alterations/' + filename }))
);
it('exits with code 1 when no alteration file available', async () => {
jest.spyOn(process, 'exit').mockImplementation(mockExit);
await expect(functions.chooseAlterationsByVersion([], 'v1.0.0')).rejects.toThrow('1');
mockExit.mockRestore();
});
it('chooses correct alteration files', async () => {
await Promise.all([
expect(functions.chooseAlterationsByVersion(files, 'v1.0.0')).resolves.toEqual(
files.slice(0, 7)
),
expect(functions.chooseAlterationsByVersion(files, 'v1.0.0-beta.10')).resolves.toEqual(
files.slice(0, 3)
),
expect(functions.chooseAlterationsByVersion(files, 'v1.1.0')).resolves.toEqual(
files.slice(0, 8)
),
expect(functions.chooseAlterationsByVersion(files, 'v1.2.0')).resolves.toEqual(files),
]);
});
});

View file

@ -98,7 +98,7 @@ export const getUndeployedAlterations = async (pool: DatabasePool) => {
const databaseTimestamp = await getCurrentDatabaseAlterationTimestamp(pool);
const files = await getAlterationFiles();
return files.filter(({ filename }) => getTimestampFromFilename(filename) > 0);
return files.filter(({ filename }) => getTimestampFromFilename(filename) > databaseTimestamp);
};
const deployAlteration = async (
@ -128,9 +128,8 @@ const deployAlteration = async (
const latestTag = 'latest';
// TODO: add tests
export const chooseAlterationsByVersion = async (
alterations: AlterationFile[],
alterations: readonly AlterationFile[],
initialVersion?: string
) => {
const versions = alterations
@ -142,7 +141,7 @@ export const chooseAlterationsByVersion = async (
.sort((i, j) => compare(j, i));
if (!versions[0]) {
log.error('No deployable alteration found');
log.error('No alteration script to deploy');
}
const { version: targetVersion } =
@ -153,8 +152,8 @@ export const chooseAlterationsByVersion = async (
type: 'list',
message: 'Choose the alteration target version',
name: 'version',
choices: versions.map((semVersion, index) => ({
name: semVersion.version + conditionalString(!index && ` (${latestTag})`),
choices: versions.map((semVersion) => ({
name: semVersion.version,
value: semVersion,
})),
},
@ -163,6 +162,8 @@ export const chooseAlterationsByVersion = async (
}
);
log.info(`Deploy target ${chalk.green(targetVersion.version)}`);
return alterations.filter(({ filename }) => {
const version = getVersionFromFilename(filename);

View file

@ -6,9 +6,11 @@ import { hideBin } from 'yargs/helpers';
import connector from './commands/connector';
import database from './commands/database';
import install from './commands/install';
import packageJson from './package.json';
import { cliConfig, ConfigKey } from './utilities';
void yargs(hideBin(process.argv))
.version(false)
.option('env', {
alias: ['e', 'env-file'],
describe: 'The path to your `.env` file',
@ -19,6 +21,19 @@ void yargs(hideBin(process.argv))
describe: 'The Postgres URL to Logto database',
type: 'string',
})
.option('version', {
alias: 'v',
describe: 'Print Logto CLI version',
type: 'boolean',
global: false,
})
.middleware(({ version }) => {
if (version) {
console.log(packageJson.name + ' v' + packageJson.version);
// eslint-disable-next-line unicorn/no-process-exit
process.exit(0);
}
}, true)
.middleware(({ env, db: databaseUrl }) => {
dotenv.config({ path: env });

View file

@ -0,0 +1 @@
../package.json