-
Notifications
You must be signed in to change notification settings - Fork 176
/
Copy pathmigrationsDir.js
137 lines (118 loc) · 4.08 KB
/
migrationsDir.js
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
const fs = require("fs-extra");
const path = require("path");
const url = require("url");
const crypto = require("crypto");
const config = require("./config");
const moduleLoader = require('../utils/module-loader');
const DEFAULT_MIGRATIONS_DIR_NAME = "migrations";
const DEFAULT_MIGRATION_EXT = ".js";
async function resolveMigrationsDirPath() {
let migrationsDir;
try {
const configContent = await config.read();
migrationsDir = configContent.migrationsDir; // eslint-disable-line
// if config file doesn't have migrationsDir key, assume default 'migrations' dir
if (!migrationsDir) {
migrationsDir = DEFAULT_MIGRATIONS_DIR_NAME;
}
} catch (err) {
// config file could not be read, assume default 'migrations' dir
migrationsDir = DEFAULT_MIGRATIONS_DIR_NAME;
}
if (path.isAbsolute(migrationsDir)) {
return migrationsDir;
}
return path.join(process.cwd(), migrationsDir);
}
async function resolveMigrationFileExtension() {
let migrationFileExtension;
try {
const configContent = await config.read();
migrationFileExtension = configContent.migrationFileExtension || DEFAULT_MIGRATION_EXT;
} catch (err) {
// config file could not be read, assume default extension
migrationFileExtension = DEFAULT_MIGRATION_EXT;
}
if (migrationFileExtension && !migrationFileExtension.startsWith('.')) {
throw new Error('migrationFileExtension must start with dot');
}
return migrationFileExtension;
}
async function resolveSampleMigrationFileName() {
const migrationFileExtention = await resolveMigrationFileExtension();
return `sample-migration${migrationFileExtention}`;
}
async function resolveSampleMigrationPath() {
const migrationsDir = await resolveMigrationsDirPath();
const sampleMigrationSampleFileName = await resolveSampleMigrationFileName();
return path.join(migrationsDir, sampleMigrationSampleFileName);
}
module.exports = {
resolve: resolveMigrationsDirPath,
resolveSampleMigrationPath,
resolveMigrationFileExtension,
async shouldExist() {
const migrationsDir = await resolveMigrationsDirPath();
try {
await fs.stat(migrationsDir);
} catch (err) {
throw new Error(`migrations directory does not exist: ${migrationsDir}`);
}
},
async shouldNotExist() {
const migrationsDir = await resolveMigrationsDirPath();
const error = new Error(
`migrations directory already exists: ${migrationsDir}`
);
try {
await fs.stat(migrationsDir);
throw error;
} catch (err) {
if (err.code !== "ENOENT") {
throw error;
}
}
},
async getFileNames() {
const migrationsDir = await resolveMigrationsDirPath();
const migrationExt = await resolveMigrationFileExtension();
const files = await fs.readdir(migrationsDir);
const sampleMigrationFileName = await resolveSampleMigrationFileName();
return files.filter(file => path.extname(file) === migrationExt && path.basename(file) !== sampleMigrationFileName).sort();
},
async loadMigration(fileName) {
const migrationsDir = await resolveMigrationsDirPath();
const migrationPath = path.join(migrationsDir, fileName);
try {
const result = moduleLoader.require(migrationPath);
return getModuleExports(result);
} catch (e) {
if (e.code === 'ERR_REQUIRE_ESM') {
const loadedImport = moduleLoader.import(url.pathToFileURL(migrationPath));
return getModuleExports(loadedImport);
}
throw e;
}
},
async loadFileHash(fileName) {
const migrationsDir = await resolveMigrationsDirPath();
const filePath = path.join(migrationsDir, fileName)
const hash = crypto.createHash('sha256');
const input = await fs.readFile(filePath);
hash.update(input);
return hash.digest('hex');
},
async doesSampleMigrationExist() {
const samplePath = await resolveSampleMigrationPath();
try {
await fs.stat(samplePath);
return true;
} catch (err) {
return false;
}
},
};
function getModuleExports(module) {
// If ESM module format need to return default export
return module.default ? module.default : module;
}