@tryghost/mg-fs-utils 0.13.2 → 0.14.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -0
- package/lib/FileCache.js +27 -5
- package/package.json +8 -7
package/README.md
CHANGED
|
@@ -11,6 +11,13 @@ or
|
|
|
11
11
|
|
|
12
12
|
## Usage
|
|
13
13
|
|
|
14
|
+
You can add `.env` file in the root of this package to set a different local cache path.
|
|
15
|
+
By default, it will create a temporary folder in the system's temp directory, but you may want to chang this to a more permanent location. Setting `CACHE_PATH` in `.env` will override the default.
|
|
16
|
+
|
|
17
|
+
```env
|
|
18
|
+
CACHE_PATH=/path/to/cache
|
|
19
|
+
```
|
|
20
|
+
|
|
14
21
|
|
|
15
22
|
## Develop
|
|
16
23
|
|
package/lib/FileCache.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {basename, dirname, extname, format, join, parse} from 'node:path';
|
|
2
|
+
import {URL} from 'node:url';
|
|
2
3
|
import {tmpdir} from 'node:os';
|
|
3
4
|
import {createHash} from 'node:crypto';
|
|
5
|
+
import * as dotenv from 'dotenv';
|
|
4
6
|
import _ from 'lodash';
|
|
5
7
|
import {writeFileSync, readdirSync, rmdir, lstatSync, existsSync} from 'node:fs';
|
|
6
8
|
import {writeFile} from 'node:fs/promises';
|
|
@@ -11,12 +13,22 @@ import transliterate from 'transliteration';
|
|
|
11
13
|
import csv from './csv.js';
|
|
12
14
|
import {cacheNameFromPath} from './utils.js';
|
|
13
15
|
|
|
16
|
+
const __dirname = new URL('.', import.meta.url).pathname;
|
|
17
|
+
|
|
18
|
+
// Define the .end path for this project, as well as parent env files
|
|
19
|
+
dotenv.config({
|
|
20
|
+
path: [
|
|
21
|
+
join(__dirname, '../', '.env'),
|
|
22
|
+
'.env'
|
|
23
|
+
]
|
|
24
|
+
});
|
|
25
|
+
|
|
14
26
|
const basePath = 'mg';
|
|
15
27
|
const knownImageExtensions = ['.jpg', '.jpeg', '.gif', '.png', '.svg', '.svgz', '.ico', 'webp'];
|
|
16
28
|
|
|
17
29
|
export default class FileCache {
|
|
18
30
|
constructor(cacheName, options = {}) {
|
|
19
|
-
this.tmpPath = options
|
|
31
|
+
this.tmpPath = options?.tmpPath ?? process?.env?.CACHE_PATH ?? false;
|
|
20
32
|
|
|
21
33
|
this.originalName = cacheName;
|
|
22
34
|
this.options = Object.assign({contentDir: true}, options);
|
|
@@ -143,10 +155,15 @@ export default class FileCache {
|
|
|
143
155
|
|
|
144
156
|
sanitizeFileName(src) {
|
|
145
157
|
let fileNameNoExt = parse(src).name;
|
|
158
|
+
let fileNameExt = extname(src);
|
|
146
159
|
|
|
147
|
-
let safeFileNameNoExt = transliterate.slugify(fileNameNoExt
|
|
160
|
+
let safeFileNameNoExt = transliterate.slugify(fileNameNoExt, {
|
|
161
|
+
separator: '_'
|
|
162
|
+
});
|
|
148
163
|
|
|
149
|
-
|
|
164
|
+
let result = src.replace(`${fileNameNoExt}${fileNameExt}`, `${safeFileNameNoExt}${fileNameExt}`);
|
|
165
|
+
|
|
166
|
+
return result;
|
|
150
167
|
}
|
|
151
168
|
|
|
152
169
|
ensureJsonExtension({filename, isJSON = true}) {
|
|
@@ -162,7 +179,12 @@ export default class FileCache {
|
|
|
162
179
|
}
|
|
163
180
|
|
|
164
181
|
resolveFileName(filename, type = 'images') {
|
|
165
|
-
|
|
182
|
+
const ext = extname(filename);
|
|
183
|
+
|
|
184
|
+
// Replace any dots in the path with dashes
|
|
185
|
+
const parsedPath = parse(filename);
|
|
186
|
+
parsedPath.dir = parsedPath.dir.replace(/\./g, '-');
|
|
187
|
+
filename = format(parsedPath);
|
|
166
188
|
|
|
167
189
|
let typeDir = null;
|
|
168
190
|
let typePath = null;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tryghost/mg-fs-utils",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.14.1",
|
|
4
4
|
"repository": "https://github.com/TryGhost/migrate/tree/main/packages/mg-fs-utils",
|
|
5
5
|
"author": "Ghost Foundation",
|
|
6
6
|
"license": "MIT",
|
|
@@ -25,16 +25,17 @@
|
|
|
25
25
|
"jest-extended": "4.0.2"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@tryghost/errors": "1.3.
|
|
29
|
-
"@tryghost/image-transform": "1.
|
|
30
|
-
"@tryghost/string": "0.2.
|
|
31
|
-
"@tryghost/zip": "1.1.
|
|
28
|
+
"@tryghost/errors": "1.3.2",
|
|
29
|
+
"@tryghost/image-transform": "1.3.0",
|
|
30
|
+
"@tryghost/string": "0.2.12",
|
|
31
|
+
"@tryghost/zip": "1.1.43",
|
|
32
32
|
"adm-zip": "0.5.12",
|
|
33
|
-
"csv-parse": "5.5.
|
|
33
|
+
"csv-parse": "5.5.6",
|
|
34
|
+
"dotenv": "16.4.5",
|
|
34
35
|
"fs-extra": "11.2.0",
|
|
35
36
|
"lodash": "4.17.21",
|
|
36
37
|
"sharp": "0.32.6",
|
|
37
38
|
"transliteration": "2.3.5"
|
|
38
39
|
},
|
|
39
|
-
"gitHead": "
|
|
40
|
+
"gitHead": "f824cede3d1123defe3e4575536dd579984f7fe2"
|
|
40
41
|
}
|