|
| 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 | +} |
0 commit comments