Skip to content

Commit b9debbe

Browse files
committed
Add migration tool for migrating legacy resources
Closes #949
1 parent b568142 commit b9debbe

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

bin/lib/cli.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const program = require('commander')
22
const loadInit = require('./init')
33
const loadStart = require('./start')
44
const loadInvalidUsernames = require('./invalidUsernames')
5+
const loadMigrateLegacyResources = require('./migrateLegacyResources')
56
const { spawnSync } = require('child_process')
67
const path = require('path')
78

@@ -11,6 +12,7 @@ module.exports = function startCli (server) {
1112
loadInit(program)
1213
loadStart(program, server)
1314
loadInvalidUsernames(program)
15+
loadMigrateLegacyResources(program)
1416

1517
program.parse(process.argv)
1618
if (program.args.length === 0) program.help()

bin/lib/migrateLegacyResources.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
const fs = require('fs')
2+
const Path = require('path')
3+
const promisify = require('util').promisify
4+
5+
/* Converts the old (pre-5.0.0) extensionless files to $-based files _with_ extensions
6+
* to make them work in the new resource mapper (post-5.0.0).
7+
* By default, all extensionless files (that used to be interpreted as Turtle) will now receive a '$.ttl' suffix. */
8+
/* https://www.w3.org/DesignIssues/HTTPFilenameMapping.html */
9+
10+
module.exports = function (program) {
11+
program
12+
.command('migrate-legacy-resources')
13+
.option('-p, --path <path>', 'Path to the data folder, defaults to \'data/\'')
14+
.option('-s, --suffix <path>', 'The suffix to add, defaults to \'$.ttl\'')
15+
.option('-v, --verbose', 'Path to the data folder')
16+
.description('Migrate extensionless data files pre-5.0.0 to turtle-based data files post-5.0.0')
17+
.action(async (opts) => {
18+
const verbose = opts.verbose
19+
const suffix = opts.suffix || '$.ttl'
20+
let path = opts.path || 'data'
21+
path = path.startsWith(Path.sep) ? path : Path.join(process.cwd(), path)
22+
if (verbose) {
23+
console.log(`Migrating files in ${path}`)
24+
}
25+
try {
26+
await migrate(path, suffix, verbose)
27+
} catch (err) {
28+
console.error(err)
29+
}
30+
})
31+
}
32+
33+
async function migrate (path, suffix, verbose) {
34+
const files = await promisify(fs.readdir)(path)
35+
for (const file of files) {
36+
const fullFilePath = Path.join(path, file)
37+
const stat = await promisify(fs.lstat)(fullFilePath)
38+
if (stat.isFile()) {
39+
if (shouldMigrateFile(file)) {
40+
const newFullFilePath = getNewFileName(fullFilePath, suffix)
41+
if (verbose) {
42+
console.log(`${fullFilePath}\n => ${newFullFilePath}`)
43+
}
44+
await promisify(fs.rename)(fullFilePath, newFullFilePath)
45+
}
46+
} else {
47+
if (shouldMigrateFolder(file)) {
48+
await migrate(fullFilePath, suffix, verbose)
49+
}
50+
}
51+
}
52+
}
53+
54+
function getNewFileName (fullFilePath, suffix) {
55+
return fullFilePath + suffix
56+
}
57+
58+
function shouldMigrateFile (filename) {
59+
return filename.indexOf('.') < 0
60+
}
61+
62+
function shouldMigrateFolder (foldername) {
63+
return foldername.indexOf('.') !== 0
64+
}

0 commit comments

Comments
 (0)