2021-10-23 13:20:35 +00:00
|
|
|
|
|
|
|
const MyVC = require('./myvc');
|
|
|
|
const fs = require('fs-extra');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new version.
|
|
|
|
*/
|
|
|
|
class Version {
|
2021-10-25 13:38:07 +00:00
|
|
|
get usage() {
|
|
|
|
return {
|
|
|
|
description: 'Creates a new version',
|
|
|
|
params: {
|
|
|
|
name: 'Name for the new version'
|
|
|
|
},
|
|
|
|
operand: 'name'
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-10-23 13:20:35 +00:00
|
|
|
get localOpts() {
|
|
|
|
return {
|
2022-02-07 10:46:13 +00:00
|
|
|
alias: {
|
2021-10-23 13:20:35 +00:00
|
|
|
name: 'n'
|
|
|
|
},
|
2022-02-07 10:46:13 +00:00
|
|
|
string: [
|
|
|
|
'name'
|
|
|
|
],
|
2021-10-23 13:20:35 +00:00
|
|
|
default: {
|
|
|
|
remote: 'production'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async run(myvc, opts) {
|
|
|
|
let versionDir;
|
|
|
|
|
|
|
|
// Fetch last version number
|
|
|
|
|
|
|
|
const conn = await myvc.dbConnect();
|
|
|
|
|
|
|
|
try {
|
|
|
|
await conn.query('START TRANSACTION');
|
|
|
|
|
|
|
|
const [[row]] = await conn.query(
|
2021-10-25 13:38:07 +00:00
|
|
|
`SELECT number, lastNumber
|
|
|
|
FROM version
|
|
|
|
WHERE code = ?
|
|
|
|
FOR UPDATE`,
|
2021-10-23 13:20:35 +00:00
|
|
|
[opts.code]
|
|
|
|
);
|
2021-10-25 13:38:07 +00:00
|
|
|
const number = row && row.number;
|
|
|
|
const lastNumber = row && row.lastNumber;
|
2021-10-23 13:20:35 +00:00
|
|
|
|
|
|
|
console.log(
|
|
|
|
`Database information:`
|
2021-10-25 13:38:07 +00:00
|
|
|
+ `\n -> Version: ${number}`
|
|
|
|
+ `\n -> Last version: ${lastNumber}`
|
2021-10-23 13:20:35 +00:00
|
|
|
);
|
|
|
|
|
2021-10-25 13:38:07 +00:00
|
|
|
let newVersion;
|
|
|
|
if (lastNumber)
|
|
|
|
newVersion = Math.max(
|
2022-04-04 18:53:18 +00:00
|
|
|
parseInt(number) || 0,
|
|
|
|
parseInt(lastNumber) || 0
|
2021-10-25 13:38:07 +00:00
|
|
|
) + 1;
|
|
|
|
else
|
|
|
|
newVersion = 1;
|
|
|
|
|
|
|
|
const versionDigits = number
|
|
|
|
? number.length
|
|
|
|
: opts.versionDigits;
|
|
|
|
|
|
|
|
newVersion = String(newVersion).padStart(versionDigits, '0');
|
2021-10-23 13:20:35 +00:00
|
|
|
|
|
|
|
// Get version name
|
|
|
|
|
2021-10-25 13:38:07 +00:00
|
|
|
let versionName = opts.name;
|
|
|
|
const verionsDir =`${opts.myvcDir}/versions`;
|
|
|
|
|
2021-10-23 13:20:35 +00:00
|
|
|
const versionNames = new Set();
|
|
|
|
const versionDirs = await fs.readdir(verionsDir);
|
|
|
|
for (const versionNameDir of versionDirs) {
|
|
|
|
const split = versionNameDir.split('-');
|
|
|
|
const versionName = split[1];
|
|
|
|
if (versionName) versionNames.add(versionName);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!versionName) {
|
|
|
|
let attempts;
|
|
|
|
const maxAttempts = 1000;
|
|
|
|
|
|
|
|
for (attempts = 0; attempts < maxAttempts; attempts++) {
|
|
|
|
versionName = randomName();
|
|
|
|
if (!versionNames.has(versionName)) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (attempts === maxAttempts)
|
|
|
|
throw new Error(`Cannot create a unique version name after ${attempts} attempts`);
|
|
|
|
} else {
|
|
|
|
const isNameValid = typeof versionName === 'string'
|
|
|
|
&& /^[a-zA-Z0-9]+$/.test(versionName);
|
|
|
|
if (!isNameValid)
|
|
|
|
throw new Error('Version name can only contain letters or numbers');
|
|
|
|
if (versionNames.has(versionName))
|
|
|
|
throw new Error('Version with same name already exists');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create version
|
|
|
|
|
|
|
|
const versionFolder = `${newVersion}-${versionName}`;
|
|
|
|
versionDir = `${verionsDir}/${versionFolder}`;
|
|
|
|
|
|
|
|
await conn.query(
|
2021-10-25 13:38:07 +00:00
|
|
|
`INSERT INTO version
|
|
|
|
SET code = ?,
|
|
|
|
lastNumber = ?
|
|
|
|
ON DUPLICATE KEY UPDATE
|
|
|
|
lastNumber = VALUES(lastNumber)`,
|
|
|
|
[opts.code, newVersion]
|
2021-10-23 13:20:35 +00:00
|
|
|
);
|
|
|
|
await fs.mkdir(versionDir);
|
2021-10-25 13:38:07 +00:00
|
|
|
await fs.writeFile(
|
|
|
|
`${versionDir}/00-firstScript.sql`,
|
2021-10-26 11:03:15 +00:00
|
|
|
'-- Place your SQL code here\n'
|
2021-10-25 13:38:07 +00:00
|
|
|
);
|
|
|
|
console.log(`New version created: ${versionFolder}`);
|
2021-10-23 13:20:35 +00:00
|
|
|
|
|
|
|
await conn.query('COMMIT');
|
|
|
|
} catch (err) {
|
|
|
|
await conn.query('ROLLBACK');
|
|
|
|
if (versionDir && await fs.pathExists(versionDir))
|
|
|
|
await fs.remove(versionDir, {recursive: true});
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function randomName() {
|
|
|
|
const color = random(colors);
|
|
|
|
let plant = random(plants);
|
|
|
|
plant = plant.charAt(0).toUpperCase() + plant.slice(1);
|
|
|
|
return color + plant;
|
|
|
|
}
|
|
|
|
|
|
|
|
function random(array) {
|
|
|
|
return array[Math.floor(Math.random() * array.length)];
|
|
|
|
}
|
|
|
|
|
|
|
|
const colors = [
|
|
|
|
'aqua',
|
|
|
|
'azure',
|
|
|
|
'black',
|
|
|
|
'blue',
|
|
|
|
'bronze',
|
|
|
|
'brown',
|
|
|
|
'chocolate',
|
|
|
|
'crimson',
|
|
|
|
'golden',
|
|
|
|
'gray',
|
|
|
|
'green',
|
|
|
|
'lime',
|
|
|
|
'maroon',
|
|
|
|
'navy',
|
|
|
|
'orange',
|
|
|
|
'pink',
|
|
|
|
'purple',
|
|
|
|
'red',
|
|
|
|
'salmon',
|
|
|
|
'silver',
|
|
|
|
'teal',
|
|
|
|
'turquoise',
|
|
|
|
'yellow',
|
|
|
|
'wheat',
|
|
|
|
'white'
|
|
|
|
];
|
|
|
|
|
|
|
|
const plants = [
|
|
|
|
'anthurium',
|
|
|
|
'aralia',
|
|
|
|
'arborvitae',
|
|
|
|
'asparagus',
|
|
|
|
'aspidistra',
|
|
|
|
'bamboo',
|
|
|
|
'birch',
|
|
|
|
'carnation',
|
|
|
|
'camellia',
|
|
|
|
'cataractarum',
|
|
|
|
'chico',
|
|
|
|
'chrysanthemum',
|
|
|
|
'cordyline',
|
|
|
|
'cyca',
|
|
|
|
'cymbidium',
|
|
|
|
'dendro',
|
|
|
|
'dracena',
|
|
|
|
'erica',
|
|
|
|
'eucalyptus',
|
|
|
|
'fern',
|
|
|
|
'galax',
|
|
|
|
'gerbera',
|
|
|
|
'hydrangea',
|
|
|
|
'ivy',
|
|
|
|
'laurel',
|
|
|
|
'lilium',
|
|
|
|
'mastic',
|
|
|
|
'medeola',
|
|
|
|
'monstera',
|
|
|
|
'moss',
|
|
|
|
'oak',
|
|
|
|
'orchid',
|
|
|
|
'palmetto',
|
|
|
|
'paniculata',
|
|
|
|
'phormium',
|
|
|
|
'raphis',
|
|
|
|
'roebelini',
|
|
|
|
'rose',
|
|
|
|
'ruscus',
|
|
|
|
'salal',
|
|
|
|
'tulip'
|
|
|
|
];
|
|
|
|
|
|
|
|
module.exports = Version;
|
|
|
|
|
|
|
|
if (require.main === module)
|
|
|
|
new MyVC().run(Version);
|