-
-
Notifications
You must be signed in to change notification settings - Fork 184
Expand file tree
/
Copy pathcreate.js
More file actions
39 lines (35 loc) · 1.34 KB
/
create.js
File metadata and controls
39 lines (35 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
const fs = require("fs-extra");
const path = require("path");
const date = require("../utils/date");
const migrationsDir = require("../env/migrationsDir");
const config = require("../env/config");
const _ = require("lodash");
module.exports = async description => {
if (!description) {
throw new Error("Missing parameter: description");
}
await migrationsDir.shouldExist();
const migrationsDirPath = await migrationsDir.resolve();
const migrationExtension = await migrationsDir.resolveMigrationFileExtension();
// Check if there is a 'sample-migration.js' file in migrations dir - if there is, use that
let source;
if (await migrationsDir.doesSampleMigrationExist()) {
source = await migrationsDir.resolveSampleMigrationPath();
} else {
const configContent = await config.read();
source = path.join(__dirname, `../../samples/${configContent.moduleSystem}/migration.js`);
}
let formatting;
try {
const configContent = await config.read();
formatting = _.get(configContent, "timestampFormat");
} catch (err) {
// config file could not be read, assume default formatting
}
const filename = `${date.nowAsString(formatting)}-${description
.split(" ")
.join("_")}${migrationExtension}`;
const destination = path.join(migrationsDirPath, filename);
await fs.copy(source, destination);
return filename;
};