Skip to content

Commit 1aa9860

Browse files
authored
Merge pull request #961 from rubensworks/feature/migrate-resources
Migrate resources
2 parents 8a21094 + b3586f0 commit 1aa9860

File tree

6 files changed

+72
-3
lines changed

6 files changed

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

test/integration/account-creation-oidc-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ describe('AccountManager (OIDC account creation tests)', function () {
129129
}
130130
var domain = host.split(':')[0]
131131
var card = read(path.join('accounts/nicola.' + domain,
132-
'profile/card'))
132+
'profile/card$.ttl'))
133133
var cardAcl = read(path.join('accounts/nicola.' + domain,
134134
'profile/card.acl'))
135135
var prefs = read(path.join('accounts/nicola.' + domain,

test/integration/account-manager-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ describe('AccountManager', () => {
128128
expect(found).to.be.true
129129
})
130130
.then(() => {
131-
let profile = fs.readFileSync(path.join(accountDir, '/profile/card'), 'utf8')
131+
let profile = fs.readFileSync(path.join(accountDir, '/profile/card$.ttl'), 'utf8')
132132
expect(profile).to.include('"Alice Q."')
133133

134134
let rootAcl = fs.readFileSync(path.join(accountDir, '.acl'), 'utf8')

test/integration/account-template-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ describe('AccountTemplate', () => {
4646
return template.processAccount(accountPath)
4747
})
4848
.then(() => {
49-
let profile = fs.readFileSync(path.join(accountPath, '/profile/card'), 'utf8')
49+
let profile = fs.readFileSync(path.join(accountPath, '/profile/card$.ttl'), 'utf8')
5050
expect(profile).to.include('"Alice Q."')
5151

5252
let rootAcl = fs.readFileSync(path.join(accountPath, '.acl'), 'utf8')

0 commit comments

Comments
 (0)