-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
112 lines (99 loc) · 3.29 KB
/
index.js
File metadata and controls
112 lines (99 loc) · 3.29 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
const downloadFile = require('./lib/download-file');
const escapeRegex = require('./lib/escape-regex');
const { findAndReplaceInEntry } = require('./lib/find-and-replace');
const path = require('path');
const pkg = require('./package.json');
module.exports.name = pkg.name;
module.exports.options = {
assetPath: {
default: 'assets'
},
maximumSearchDepth: {
default: 5
},
publicUrl: {
default: '/assets'
}
};
module.exports.transform = async ({ data, debug, log, options }) => {
// Creating a Map where keys are regular expressions that catch URLs with or
// without protocol, and values are objects with `assetPath` and `publicUrl`
// keys.
const assets = data.objects.reduce((result, entry) => {
const { __metadata: meta } = entry;
if (!meta || meta.modelName !== '__asset') {
return result;
}
const protocol = new URL(entry.url).protocol;
const escapedUrl = escapeRegex(entry.url.replace(protocol, ''));
const regExp = new RegExp(`(http:|https:){0,1}${escapedUrl}`, 'gi');
const assetPath =
typeof options.assetPath === 'function' ? options.assetPath(null, entry) : path.join(options.assetPath, entry.fileName);
const publicUrl =
typeof options.publicUrl === 'function' ? options.publicUrl(null, entry, assetPath) : `${options.publicUrl}/${entry.fileName}`;
result.set(regExp, { assetPath, publicUrl });
return result;
}, new Map());
const filesToDownload = {};
const entries = data.objects.map(entry => {
const replacedFields = findAndReplaceInEntry({
assets,
debug,
entry,
filesToDownload,
options
});
return {
...entry,
...replacedFields
};
});
const fileQueue = Object.keys(filesToDownload).map(url => {
return downloadFile({ localPath: filesToDownload[url], log, url });
});
await Promise.all(fileQueue);
return {
...data,
objects: entries
};
};
module.exports.getOptionsFromSetup = ({ answers }) => {
const assetPathFunctionBody = `
return [
"${answers.assetPath}",
[asset.__metadata.id, asset.fileName].join("-")
].join("/");
`;
const publicUrlFunctionBody = `
return [
"${answers.publicUrl}",
[asset.__metadata.id, asset.fileName].join("-")
].join("/");
`;
return {
assetPath: new Function('entry', 'asset', assetPathFunctionBody.trim()),
publicUrl: new Function('entry', 'asset', 'assetPath', publicUrlFunctionBody.trim())
};
};
module.exports.getSetup = ({ inquirer }) => {
return [
{
type: 'list',
name: 'assetPath',
message: 'Choose a location for the downloaded assets',
choices: ['assets', 'images', new inquirer.Separator(), { name: 'Other', value: undefined }]
},
{
when: ({ assetPath }) => assetPath === undefined,
type: 'input',
name: 'assetPath',
message: 'Type a location for the downloaded assets'
},
{
default: ({ assetPath }) => `/${assetPath}`,
type: 'input',
name: 'publicUrl',
message: 'Choose the public URL for the assets'
}
];
};