penrose 0.2.41 → 0.2.43

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.
@@ -0,0 +1,270 @@
1
+ import _ from 'lodash';
2
+ import Promise from 'bluebird';
3
+ import fs from 'fs-extra';
4
+ import globPromise from 'glob-promise';
5
+ import path from 'path';
6
+ import {describe, expect, test} from 'vitest';
7
+ import {Penrose} from '../penrose';
8
+
9
+ const dir = import.meta.dirname;
10
+
11
+ describe('Penrose', () => {
12
+ const config = {
13
+ schemes: {
14
+ public: {
15
+ path: path.join(dir, 'data/files') + path.sep
16
+ }
17
+ },
18
+ styles: {
19
+ small: {
20
+ actions: [
21
+ {
22
+ name: 'resize',
23
+ width: 480
24
+ }
25
+ ],
26
+ quality: 75
27
+ }
28
+ }
29
+ };
30
+
31
+ const penrose = new Penrose(config);
32
+
33
+ /**
34
+ *
35
+ * @param {Array} patterns
36
+ * @param {Object} options
37
+ * @returns {Promise}
38
+ */
39
+ function multiGlob(patterns, options) {
40
+ let matches = [];
41
+
42
+ return Promise.mapSeries(patterns, function (pattern) {
43
+ return globPromise(pattern, options).then(function (files) {
44
+ matches = matches.concat(files);
45
+ });
46
+ }).then(() => {
47
+ return Promise.resolve(matches);
48
+ });
49
+ }
50
+
51
+ describe('resolvePath', () => {
52
+ test('Should return expected path if URI has scheme', () => {
53
+ const actual = penrose.resolvePath('public://dir/file.jpg');
54
+ const expected = config.schemes.public.path + 'dir/file.jpg';
55
+
56
+ expect(actual).toEqual(expected);
57
+ });
58
+
59
+ test('Should return expected path if URI has no scheme', () => {
60
+ const actual = penrose.resolvePath('dir/file.jpg');
61
+ const expected = 'dir/file.jpg';
62
+
63
+ expect(actual).toEqual(expected);
64
+ });
65
+
66
+ test('Should throw error if URI has unsupported scheme', () => {
67
+ expect(() => penrose.resolvePath('http://dir/file.jpg')).toThrowError(
68
+ 'Scheme `http` not supported'
69
+ );
70
+ });
71
+ });
72
+
73
+ describe('getScheme', () => {
74
+ test('Should return expected scheme if URI has scheme', () => {
75
+ const actual = penrose.getScheme('public://dir/file.jpg');
76
+ const expected = 'public';
77
+
78
+ expect(actual).toEqual(expected);
79
+ });
80
+
81
+ test('Should return undefined scheme if URI has no scheme', () => {
82
+ const actual = penrose.getScheme('dir/file.jpg');
83
+ const expected = undefined;
84
+
85
+ expect(actual).toEqual(expected);
86
+ });
87
+ });
88
+
89
+ describe('getTarget', () => {
90
+ test('Should return expected target if URI has scheme', () => {
91
+ const actual = penrose.getTarget('public://dir/file.jpg');
92
+ const expected = 'dir/file.jpg';
93
+
94
+ expect(actual).toEqual(expected);
95
+ });
96
+
97
+ test('Should return expected target if URI has no scheme', () => {
98
+ const actual = penrose.getTarget('dir/file.jpg');
99
+ const expected = 'dir/file.jpg';
100
+
101
+ expect(actual).toEqual(expected);
102
+ });
103
+ });
104
+
105
+ describe('getURL', () => {
106
+ test('Should return expected URL if URI has scheme', () => {
107
+ const actual = penrose.getURL('public://dir/file.jpg');
108
+ const expected = '/' + config.schemes.public.path + 'dir/file.jpg';
109
+
110
+ expect(actual).toEqual(expected);
111
+ });
112
+
113
+ test('Should return expected URL if URI has no scheme', () => {
114
+ const actual = penrose.getURL('dir/file.jpg');
115
+ const expected = 'dir/file.jpg';
116
+
117
+ expect(actual).toEqual(expected);
118
+ });
119
+
120
+ test('Should return expected URL if URI has unsupported scheme', () => {
121
+ const actual = penrose.getURL('http://dir/file.jpg');
122
+ const expected = 'http://dir/file.jpg';
123
+
124
+ expect(actual).toEqual(expected);
125
+ });
126
+ });
127
+
128
+ describe('getStylePath', () => {
129
+ test('Should return expected path if URI has scheme', () => {
130
+ const actual = penrose.getStylePath('small', 'private://dir/file.jpg');
131
+ const expected = 'private://styles/small/dir/file.jpg';
132
+
133
+ expect(actual).toEqual(expected);
134
+ });
135
+
136
+ test('Should return expected path if URI has no scheme', () => {
137
+ const actual = penrose.getStylePath('small', 'dir/file.jpg');
138
+ const expected = 'public://styles/small/dir/file.jpg';
139
+
140
+ expect(actual).toEqual(expected);
141
+ });
142
+ });
143
+
144
+ describe('getStyleURL', () => {
145
+ test('Should return expected URL if URI has scheme', () => {
146
+ const actual = penrose.getStyleURL('small', 'public://dir/file.jpg');
147
+ const expected =
148
+ '/' + config.schemes.public.path + 'styles/small/dir/file.jpg';
149
+
150
+ expect(actual).toEqual(expected);
151
+ });
152
+
153
+ test('Should return expected URL if URI has no scheme', () => {
154
+ const actual = penrose.getStyleURL('small', 'dir/file.jpg');
155
+ const expected =
156
+ '/' + config.schemes.public.path + 'styles/small/dir/file.jpg';
157
+
158
+ expect(actual).toEqual(expected);
159
+ });
160
+
161
+ test('Should throw error if URI has unsupported scheme', () => {
162
+ expect(() =>
163
+ penrose.getStyleURL('small', 'http://dir/file.jpg')
164
+ ).toThrowError('Scheme `http` not supported');
165
+ });
166
+ });
167
+
168
+ describe('createDerivative', () => {
169
+ test('Should create derivative images', async () => {
170
+ await fs.remove(path.join(dir, 'data/files/styles'));
171
+
172
+ const files = await multiGlob(
173
+ _.map(config.schemes, function (scheme) {
174
+ return scheme.path + '**/*';
175
+ }),
176
+ {
177
+ nodir: true
178
+ }
179
+ );
180
+
181
+ const tasks = [];
182
+
183
+ _.forEach(files, function (file) {
184
+ _.forEach(config.styles, function (style, styleName) {
185
+ tasks.push({
186
+ style: style,
187
+ src: file,
188
+ dist: penrose.getStylePath(styleName, file)
189
+ });
190
+ });
191
+ });
192
+
193
+ await Promise.mapSeries(tasks, function (task) {
194
+ return penrose.createDerivative(task.style, task.src, task.dist);
195
+ });
196
+
197
+ const actual = await multiGlob(
198
+ _.map(config.schemes, function (scheme) {
199
+ return scheme.path + 'styles/**/*';
200
+ }),
201
+ {
202
+ nodir: true
203
+ }
204
+ );
205
+
206
+ const expected = [
207
+ path.join(
208
+ config.schemes.public.path,
209
+ 'styles/small',
210
+ config.schemes.public.path,
211
+ 'The_Earth_seen_from_Apollo_17.jpg'
212
+ )
213
+ ];
214
+
215
+ expect(actual).toEqual(expected);
216
+
217
+ await fs.remove(path.join(dir, 'data/files/styles'));
218
+ });
219
+ });
220
+
221
+ describe('createMathFile', () => {
222
+ test('Should create math files', async () => {
223
+ await fs.remove(path.join(dir, 'data/files/math'));
224
+
225
+ const math = [
226
+ {
227
+ data: 'E = mc^2',
228
+ format: 'tex'
229
+ }
230
+ ];
231
+
232
+ const tasks = [];
233
+ const expected = [];
234
+
235
+ _.forEach(math, function (value) {
236
+ const outputFormat = 'svg';
237
+ const task = {
238
+ input: value.data,
239
+ inputFormat: value.format,
240
+ outputFormat: outputFormat
241
+ };
242
+ const filename = penrose.getMathFilename(task);
243
+ task.output = penrose.getMathPath(outputFormat, filename);
244
+
245
+ tasks.push(task);
246
+
247
+ expected.push(
248
+ path.join(config.schemes.public.path, 'math/svg', filename)
249
+ );
250
+ });
251
+
252
+ await Promise.mapSeries(tasks, function (task) {
253
+ return penrose.createMathFile(task);
254
+ });
255
+
256
+ const actual = await multiGlob(
257
+ _.map(config.schemes, function (scheme) {
258
+ return scheme.path + 'math/**/*';
259
+ }),
260
+ {
261
+ nodir: true
262
+ }
263
+ );
264
+
265
+ expect(actual).toEqual(expected);
266
+
267
+ await fs.remove(path.join(dir, 'data/files/math'));
268
+ });
269
+ });
270
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "penrose",
3
- "version": "0.2.41",
3
+ "version": "0.2.43",
4
4
  "description": "Build derivative images",
5
5
  "main": "lib/penrose.js",
6
6
  "repository": {
@@ -8,7 +8,7 @@
8
8
  "url": "git://github.com/netbek/penrose"
9
9
  },
10
10
  "scripts": {
11
- "test": "make test"
11
+ "test": "vitest run"
12
12
  },
13
13
  "author": {
14
14
  "name": "Hein Bekker",
@@ -20,19 +20,19 @@
20
20
  "url": "https://github.com/netbek/penrose/issues"
21
21
  },
22
22
  "dependencies": {
23
- "@babel/core": "7.23.9",
23
+ "@babel/core": "7.26.0",
24
24
  "babel-eslint": "10.1.0",
25
- "babel-loader": "8.3.0",
25
+ "babel-loader": "8.4.1",
26
26
  "babel-plugin-lodash": "3.3.4",
27
27
  "babel-plugin-transform-class-properties": "6.24.1",
28
28
  "bluebird": "3.7.2",
29
29
  "canonical-json": "0.0.4",
30
30
  "chalk": "4.1.2",
31
31
  "cheerio": "0.22.0",
32
- "eslint": "8.56.0",
32
+ "eslint": "8.57.1",
33
33
  "eslint-config-prettier": "9.1.0",
34
- "eslint-plugin-prettier": "5.1.3",
35
- "espree": "10.0.0",
34
+ "eslint-plugin-prettier": "5.2.1",
35
+ "espree": "10.3.0",
36
36
  "fs-extra": "11.2.0",
37
37
  "glob": "7.2.3",
38
38
  "glob-promise": "4.2.2",
@@ -41,13 +41,11 @@
41
41
  "mathjax": "2.7.9",
42
42
  "mathjax-node": "0.5.2",
43
43
  "phantomjs-prebuilt": "2.1.16",
44
- "prettier": "3.2.4",
44
+ "prettier": "3.3.3",
45
45
  "replace-ext": "2.0.0",
46
- "sharp": "0.33.2"
46
+ "sharp": "0.33.5"
47
47
  },
48
48
  "devDependencies": {
49
- "chai": "4.4.1",
50
- "chai-as-promised": "7.1.1",
51
- "mocha": "9.2.2"
49
+ "vitest": "2.1.5"
52
50
  }
53
51
  }
package/Makefile DELETED
@@ -1,9 +0,0 @@
1
- GREP ?=.
2
-
3
- test: node_modules
4
- @node_modules/.bin/mocha --harmony --grep "$(GREP)"
5
-
6
- node_modules: package.json
7
- @npm install
8
-
9
- .PHONY: test
package/test/index.js DELETED
@@ -1,307 +0,0 @@
1
- const _ = require('lodash');
2
- const chai = require('chai');
3
- const {assert} = chai;
4
- const chaiAsPromised = require('chai-as-promised');
5
- chai.use(chaiAsPromised);
6
- const fs = require('fs-extra');
7
- const globPromise = require('glob-promise');
8
- const {Penrose} = require('..');
9
- const Promise = require('bluebird');
10
-
11
- Promise.promisifyAll(fs);
12
-
13
- describe('Penrose', function () {
14
- const testDir = __dirname.substring(process.cwd().length + 1) + '/';
15
-
16
- const config = {
17
- schemes: {
18
- public: {
19
- path: testDir + 'data/files/'
20
- }
21
- },
22
- styles: {
23
- small: {
24
- actions: [
25
- {
26
- name: 'resize',
27
- width: 480
28
- }
29
- ],
30
- quality: 75
31
- }
32
- }
33
- };
34
-
35
- const penrose = new Penrose(config);
36
-
37
- /**
38
- *
39
- * @param {Array} patterns
40
- * @param {Object} options
41
- * @returns {Promise}
42
- */
43
- function multiGlob(patterns, options) {
44
- let matches = [];
45
-
46
- return Promise.mapSeries(patterns, function (pattern) {
47
- return globPromise(pattern, options).then(function (files) {
48
- matches = matches.concat(files);
49
- });
50
- }).then(function () {
51
- return Promise.resolve(matches);
52
- });
53
- }
54
-
55
- /**
56
- * Deletes test output files.
57
- *
58
- * @returns {Promise}
59
- */
60
- function deleteOutput() {
61
- const dirs = _.map(config.schemes, function (scheme) {
62
- return scheme.path + 'styles/';
63
- }).concat([config.schemes.public.path + 'math/']);
64
-
65
- return Promise.mapSeries(dirs, (dir) => fs.removeAsync(dir));
66
- }
67
-
68
- beforeEach(function (done) {
69
- deleteOutput().then(function () {
70
- done();
71
- });
72
- });
73
-
74
- after(function (done) {
75
- deleteOutput().then(function () {
76
- done();
77
- });
78
- });
79
-
80
- describe('resolvePath', function () {
81
- it('Should return expected path if URI has scheme', function () {
82
- const actual = penrose.resolvePath('public://dir/file.jpg');
83
- const expected = config.schemes.public.path + 'dir/file.jpg';
84
-
85
- assert.equal(actual, expected);
86
- });
87
-
88
- it('Should return expected path if URI has no scheme', function () {
89
- const actual = penrose.resolvePath('dir/file.jpg');
90
- const expected = 'dir/file.jpg';
91
-
92
- assert.equal(actual, expected);
93
- });
94
-
95
- it('Should throw error if URI has unsupported scheme', function () {
96
- let actual;
97
- const expected = 'error';
98
-
99
- try {
100
- actual = penrose.resolvePath('http://dir/file.jpg');
101
- } catch (e) {
102
- actual = e;
103
- }
104
-
105
- assert.typeOf(actual, expected);
106
- });
107
- });
108
-
109
- describe('getScheme', function () {
110
- it('Should return expected scheme if URI has scheme', function () {
111
- const actual = penrose.getScheme('public://dir/file.jpg');
112
- const expected = 'public';
113
-
114
- assert.equal(actual, expected);
115
- });
116
-
117
- it('Should return undefined scheme if URI has no scheme', function () {
118
- const actual = penrose.getScheme('dir/file.jpg');
119
- const expected = undefined;
120
-
121
- assert.equal(actual, expected);
122
- });
123
- });
124
-
125
- describe('getTarget', function () {
126
- it('Should return expected target if URI has scheme', function () {
127
- const actual = penrose.getTarget('public://dir/file.jpg');
128
- const expected = 'dir/file.jpg';
129
-
130
- assert.equal(actual, expected);
131
- });
132
-
133
- it('Should return expected target if URI has no scheme', function () {
134
- const actual = penrose.getTarget('dir/file.jpg');
135
- const expected = 'dir/file.jpg';
136
-
137
- assert.equal(actual, expected);
138
- });
139
- });
140
-
141
- describe('getURL', function () {
142
- it('Should return expected URL if URI has scheme', function () {
143
- const actual = penrose.getURL('public://dir/file.jpg');
144
- const expected = '/' + config.schemes.public.path + 'dir/file.jpg';
145
-
146
- assert.equal(actual, expected);
147
- });
148
-
149
- it('Should return expected URL if URI has no scheme', function () {
150
- const actual = penrose.getURL('dir/file.jpg');
151
- const expected = 'dir/file.jpg';
152
-
153
- assert.equal(actual, expected);
154
- });
155
-
156
- it('Should return expected URL if URI has unsupported scheme', function () {
157
- const actual = penrose.getURL('http://dir/file.jpg');
158
- const expected = 'http://dir/file.jpg';
159
-
160
- assert.equal(actual, expected);
161
- });
162
- });
163
-
164
- describe('getStylePath', function () {
165
- it('Should return expected path if URI has scheme', function () {
166
- const actual = penrose.getStylePath('small', 'private://dir/file.jpg');
167
- const expected = 'private://styles/small/dir/file.jpg';
168
-
169
- assert.equal(actual, expected);
170
- });
171
-
172
- it('Should return expected path if URI has no scheme', function () {
173
- const actual = penrose.getStylePath('small', 'dir/file.jpg');
174
- const expected = 'public://styles/small/dir/file.jpg';
175
-
176
- assert.equal(actual, expected);
177
- });
178
- });
179
-
180
- describe('getStyleURL', function () {
181
- it('Should return expected URL if URI has scheme', function () {
182
- const actual = penrose.getStyleURL('small', 'public://dir/file.jpg');
183
- const expected =
184
- '/' + config.schemes.public.path + 'styles/small/dir/file.jpg';
185
-
186
- assert.equal(actual, expected);
187
- });
188
-
189
- it('Should return expected URL if URI has no scheme', function () {
190
- const actual = penrose.getStyleURL('small', 'dir/file.jpg');
191
- const expected =
192
- '/' + config.schemes.public.path + 'styles/small/dir/file.jpg';
193
-
194
- assert.equal(actual, expected);
195
- });
196
-
197
- it('Should throw error if URI has unsupported scheme', function () {
198
- let actual;
199
- const expected = 'error';
200
-
201
- try {
202
- actual = penrose.getStyleURL('small', 'http://dir/file.jpg');
203
- } catch (e) {
204
- actual = e;
205
- }
206
-
207
- assert.typeOf(actual, expected);
208
- });
209
- });
210
-
211
- describe('createDerivative', function () {
212
- it('Should create derivative images', function () {
213
- const actual = function () {
214
- return multiGlob(
215
- _.map(config.schemes, function (scheme) {
216
- return scheme.path + '**/*';
217
- }),
218
- {
219
- nodir: true
220
- }
221
- )
222
- .then(function (files) {
223
- const tasks = [];
224
-
225
- _.forEach(files, function (file) {
226
- _.forEach(config.styles, function (style, styleName) {
227
- tasks.push({
228
- style: style,
229
- src: file,
230
- dist: penrose.getStylePath(styleName, file)
231
- });
232
- });
233
- });
234
-
235
- return Promise.mapSeries(tasks, function (task) {
236
- return penrose.createDerivative(task.style, task.src, task.dist);
237
- });
238
- })
239
- .then(function () {
240
- return multiGlob(
241
- _.map(config.schemes, function (scheme) {
242
- return scheme.path + 'styles/**/*';
243
- }),
244
- {
245
- nodir: true
246
- }
247
- );
248
- });
249
- };
250
-
251
- const expected = [
252
- config.schemes.public.path +
253
- 'styles/small/' +
254
- config.schemes.public.path +
255
- 'The_Earth_seen_from_Apollo_17.jpg'
256
- ];
257
-
258
- return assert.eventually.deepEqual(actual(), expected);
259
- });
260
- });
261
-
262
- describe('createMathFile', function () {
263
- it('Should create math files', function () {
264
- const math = [
265
- {
266
- data: 'E = mc^2',
267
- format: 'tex'
268
- }
269
- ];
270
-
271
- const tasks = [];
272
- const expected = [];
273
-
274
- _.forEach(math, function (value) {
275
- const outputFormat = 'svg';
276
- const task = {
277
- input: value.data,
278
- inputFormat: value.format,
279
- outputFormat: outputFormat
280
- };
281
- const filename = penrose.getMathFilename(task);
282
- task.output = penrose.getMathPath(outputFormat, filename);
283
-
284
- tasks.push(task);
285
-
286
- expected.push(config.schemes.public.path + 'math/svg/' + filename);
287
- });
288
-
289
- const actual = function () {
290
- return Promise.mapSeries(tasks, function (task) {
291
- return penrose.createMathFile(task);
292
- }).then(function () {
293
- return multiGlob(
294
- _.map(config.schemes, function (scheme) {
295
- return scheme.path + 'math/**/*';
296
- }),
297
- {
298
- nodir: true
299
- }
300
- );
301
- });
302
- };
303
-
304
- return assert.eventually.deepEqual(actual(), expected);
305
- });
306
- });
307
- });