Skip to content

Commit 9ba4c52

Browse files
author
Alexander Yukal
committed
improve: core
1 parent c490f8f commit 9ba4c52

File tree

1 file changed

+58
-47
lines changed

1 file changed

+58
-47
lines changed

lib/gulp-json-loader.js

Lines changed: 58 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,14 @@ const Fs = require('fs');
1717
const APP_PATH = process.cwd();
1818
const DEFAULT_SOURCE_PATH = 'src';
1919
const BREAK_LINE = process.platform === 'win32' ? '\r\n' : '\n';
20-
const ERR_TOP_DIR = 'Working with external paths is prohibited!';
20+
const ERR_EXTERNAL_PATH = 'Working with external paths is prohibited!';
2121

2222
const BrushColors = {
2323
red: 1,
2424
cyan: 6,
2525
grey: 8
2626
};
2727

28-
const getAbsolutePath = (path, subdir = '') => {
29-
if (path.startsWith('/')) {
30-
path = '.' + path;
31-
}
32-
33-
path = Path.join(APP_PATH, path);
34-
35-
return subdir
36-
? Path.join(path, subdir)
37-
: path;
38-
}
39-
4028
/**
4129
* brush
4230
* Adding the ANSI escape codes to a textual data
@@ -75,19 +63,36 @@ const reportAction = (ctx, pathJson, filename, action) => {
7563
process.stdout.write(message);
7664
}
7765

78-
const loadImportsAsync = (ctx, imports, CachedData) => {
66+
const loadImportsAsync = (ctx, imports, cachedData) => {
7967
return new Promise((resolve, reject) => {
8068
if (!Array.isArray(imports)) {
69+
8170
reject(new Error('Imports should be an Array'));
71+
8272
} else {
83-
const pocket = {};
84-
loadImportsRecursively({ ctx, imports, pocket, CachedData, resolve, reject });
73+
74+
if (imports.length < 1) {
75+
resolve({});
76+
return;
77+
}
78+
79+
const options = { ctx, imports, cachedData, pocket: {} };
80+
81+
loadImportsRecursively(options, (error, pocket) => {
82+
if (error !== null) {
83+
reject(error);
84+
return;
85+
}
86+
87+
resolve(pocket);
88+
});
89+
8590
}
8691
});
8792
};
8893

89-
const loadImportsRecursively = async (options) => {
90-
const { ctx, imports, pocket, CachedData, resolve, reject, idx = 0 } = options;
94+
const loadImportsRecursively = async (options, callback) => {
95+
const { ctx, imports, cachedData, pocket, idx = 0 } = options;
9196
const { report, pathData } = ctx;
9297

9398
if (idx < imports.length) {
@@ -99,21 +104,21 @@ const loadImportsRecursively = async (options) => {
99104
const cacheKey = jsonFullPath.replace(`${APP_PATH}/`, '');
100105

101106
if (!jsonFullPath.startsWith(APP_PATH)) {
102-
reject(new Error(ERR_TOP_DIR));
107+
callback(new Error(ERR_EXTERNAL_PATH), null);
103108
return;
104109
}
105110

106-
if (!CachedData.hasOwnProperty(cacheKey)) {
111+
if (!cachedData.hasOwnProperty(cacheKey)) {
107112
try {
108113

109114
const jsonData = await Fs.promises.readFile(jsonFullPath, 'utf8');
110115

111-
CachedData[cacheKey] = JSON.parse(jsonData);
116+
cachedData[cacheKey] = JSON.parse(jsonData);
112117
action = 'Loaded';
113118

114119
} catch (error) {
115120

116-
reject(error);
121+
callback(error, null);
117122
return;
118123

119124
}
@@ -122,52 +127,52 @@ const loadImportsRecursively = async (options) => {
122127
report && reportAction(ctx, jsonFullPath, jsonFilename, action);
123128

124129
Object.defineProperty(pocket, jsonFilename, {
125-
value: CachedData[cacheKey],
130+
value: cachedData[cacheKey],
126131
enumerable: true
127132
});
128133

129134
options.idx = idx + 1;
130-
loadImportsRecursively(options);
135+
loadImportsRecursively(options, callback);
131136

132-
} else {
133-
resolve(pocket);
137+
} else if (callback !== undefined) {
138+
callback(null, pocket);
134139
}
135140
}
136141

137142
async function loadJsonData(file) {
138-
const { report, pathHtml, pathData, dataEntry, CachedData } = this;
143+
const { report, pathHtml, pathData, dataEntry, cachedData } = this;
139144

140-
const pathJson = file.path
145+
const jsonFullPath = file.path
141146
.replace(pathHtml, `${pathData}/pages`)
142147
.slice(0, -3) + 'json';
143148

144-
const cacheKey = pathJson.replace(`${APP_PATH}/`, '');
149+
const cacheKey = jsonFullPath.replace(`${APP_PATH}/`, '');
145150

146151
const filename = Path.basename(file.path, '.pug');
147152
const pocket = { filename };
148153

149-
if (CachedData.hasOwnProperty(cacheKey)) {
150-
report && reportAction(this, pathJson, filename, 'Cached');
151-
return CachedData[cacheKey];
154+
if (cachedData.hasOwnProperty(cacheKey)) {
155+
report && reportAction(this, jsonFullPath, filename, 'Cached');
156+
return cachedData[cacheKey];
152157
}
153158

154159
let jsonData = '';
155160

156161
try {
157-
jsonData = await Fs.promises.readFile(pathJson, 'utf8');
162+
jsonData = await Fs.promises.readFile(jsonFullPath, 'utf8');
158163
jsonData = JSON.parse(jsonData);
159164
} catch (err) {
160165
return pocket;
161166
}
162167

163-
report && reportAction(this, pathJson, filename, 'Loaded');
168+
report && reportAction(this, jsonFullPath, filename, 'Loaded');
164169

165170
if (jsonData.hasOwnProperty('imports')) {
166171
const { data = {}, imports = [] } = jsonData;
167172

168173
try {
169174

170-
const importedData = await loadImportsAsync(this, imports, CachedData);
175+
const importedData = await loadImportsAsync(this, imports, cachedData);
171176

172177
Object.defineProperty(data, 'imports', {
173178
value: importedData,
@@ -179,7 +184,7 @@ async function loadJsonData(file) {
179184
const coloredErrorMesage = brush('red', err.message);
180185
process.stderr.write(coloredErrorMesage + BREAK_LINE);
181186

182-
return;
187+
return pocket;
183188

184189
}
185190

@@ -189,7 +194,7 @@ async function loadJsonData(file) {
189194
});
190195
}
191196

192-
CachedData[cacheKey] = pocket;
197+
cachedData[cacheKey] = pocket;
193198

194199
return pocket;
195200
}
@@ -211,29 +216,36 @@ const factory = (options, testMode = false) => {
211216
: DEFAULT_SOURCE_PATH;
212217

213218
const pathHtml = options.hasOwnProperty('pathHtml')
214-
? getAbsolutePath(options.pathHtml)
215-
: getAbsolutePath(sourcePath, 'html');
219+
? Path.join(APP_PATH, options.pathHtml)
220+
: Path.join(APP_PATH, sourcePath, 'html');
216221

217222
const pathData = options.hasOwnProperty('pathData')
218-
? getAbsolutePath(options.pathData)
219-
: getAbsolutePath(sourcePath, 'data');
223+
? Path.join(APP_PATH, options.pathData)
224+
: Path.join(APP_PATH, sourcePath, 'data');
220225

221226
if (!pathHtml.startsWith(APP_PATH) || !pathData.startsWith(APP_PATH)) {
222227
// Please put your source files into your project directory.
223-
throw new Error(ERR_TOP_DIR);
228+
throw new Error(ERR_EXTERNAL_PATH);
224229
}
225230

226-
const bindData = {
231+
const context = {
227232
sourcePath,
228233
pathHtml,
229234
pathData,
230235
dataEntry,
231236
locales,
232237
report,
233-
CachedData: {}
238+
cachedData: {}
234239
};
235240

236-
return testMode ? bindData : loadJsonData.bind(bindData);
241+
if (testMode) {
242+
return {
243+
context,
244+
loader: loadJsonData.bind(context),
245+
}
246+
}
247+
248+
return loadJsonData.bind(context);
237249
}
238250

239251
factory.forTest = () => {
@@ -245,10 +257,9 @@ factory.forTest = () => {
245257
APP_PATH,
246258
DEFAULT_SOURCE_PATH,
247259
BREAK_LINE,
248-
ERR_TOP_DIR,
260+
ERR_EXTERNAL_PATH,
249261
},
250262

251-
getAbsolutePath,
252263
brush,
253264
reportAction,
254265
loadImportsAsync,

0 commit comments

Comments
 (0)