-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmigrate.js
47 lines (40 loc) · 1.48 KB
/
migrate.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
#!/usr/bin/env node
const log = require('debug')('mega-scraper:migration:01:html-json-folders')
const fs = require('fs')
const path = require('path')
const migrationsConfig = require('./migrations/config.json')
const cp = require('child_process')
if (require.main === module) {
main()
.then(() => {
process.exit(0)
})
.catch((err) => {
log(err)
process.exit(1)
})
} else {
module.exports = main
}
async function main () {
const migrationsPath = path.resolve(__dirname, 'migrations')
let migrationFilenames = fs.readdirSync(migrationsPath)
migrationFilenames = migrationFilenames
.filter(f => f !== 'config.json')
.filter(f => !migrationsConfig.migrated.find(({ filename }) => filename === f))
console.log(`migrations to run: \n${migrationFilenames.map(f => `- ${f}`).join('\n')}\n`)
for (const filename of migrationFilenames) {
console.time(`running ${path.resolve(migrationsPath, filename)}`)
try {
console.log(cp.execFileSync(path.resolve(migrationsPath, filename)).toString())
} catch (err) {
console.error(err)
}
console.timeEnd(`running ${path.resolve(migrationsPath, filename)}`)
const date = new Date().toISOString()
migrationsConfig.migrated.push({ filename, date })
}
const configPath = path.resolve(migrationsPath, 'config.json')
console.log(`writing config to ${configPath}\n${JSON.stringify(migrationsConfig, null, 2)}`)
fs.writeFileSync(configPath, JSON.stringify(migrationsConfig, null, 2))
}