forked from nodeSolidServer/node-solid-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrateLegacyResources.js
More file actions
69 lines (63 loc) · 2.31 KB
/
Copy pathmigrateLegacyResources.js
File metadata and controls
69 lines (63 loc) · 2.31 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
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
const fs = require('fs')
const Path = require('path')
const promisify = require('util').promisify
const readdir = promisify(fs.readdir)
const lstat = promisify(fs.lstat)
const rename = promisify(fs.rename)
/* Converts the old (pre-5.0.0) extensionless files to $-based files _with_ extensions
* to make them work in the new resource mapper (post-5.0.0).
* By default, all extensionless files (that used to be interpreted as Turtle) will now receive a '$.ttl' suffix. */
/* https://www.w3.org/DesignIssues/HTTPFilenameMapping.html */
module.exports = function (program) {
program
.command('migrate-legacy-resources')
.option('-p, --path <path>', 'Path to the data folder, defaults to \'data/\'')
.option('-s, --suffix <path>', 'The suffix to add to extensionless files, defaults to \'$.ttl\'')
.option('-v, --verbose', 'Path to the data folder')
.description('Migrate the data folder from node-solid-server 4 to node-solid-server 5')
.action(async (opts) => {
const verbose = opts.verbose
const suffix = opts.suffix || '$.ttl'
let paths = opts.path ? [opts.path] : ['data', 'config/templates']
paths = paths.map(path => path.startsWith(Path.sep) ? path : Path.join(process.cwd(), path))
try {
for (const path of paths) {
if (verbose) {
console.log(`Migrating files in ${path}`)
}
await migrate(path, suffix, verbose)
}
} catch (err) {
console.error(err)
}
})
}
async function migrate (path, suffix, verbose) {
const files = await readdir(path)
for (const file of files) {
const fullFilePath = Path.join(path, file)
const stat = await lstat(fullFilePath)
if (stat.isFile()) {
if (shouldMigrateFile(file)) {
const newFullFilePath = getNewFileName(fullFilePath, suffix)
if (verbose) {
console.log(`${fullFilePath}\n => ${newFullFilePath}`)
}
await rename(fullFilePath, newFullFilePath)
}
} else {
if (shouldMigrateFolder(file)) {
await migrate(fullFilePath, suffix, verbose)
}
}
}
}
function getNewFileName (fullFilePath, suffix) {
return fullFilePath + suffix
}
function shouldMigrateFile (filename) {
return filename.indexOf('.') < 0
}
function shouldMigrateFolder (foldername) {
return foldername[0] !== '.'
}