Skip to content

Commit c979bc4

Browse files
committed
create missing dir when it is not exists
1 parent cf6b93b commit c979bc4

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/index.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ export default class I18nextPlugin {
111111
protected startTime = Date.now();
112112
protected prevTimestamps: {[file: string]: number} = {};
113113
protected sourceMaps: {[key: string]: SourceMapConsumer} = {};
114+
protected missingDirInitialized = false;
114115

115116
public constructor(option: Option) {
116117
this.option = _.defaults(option, {
@@ -147,6 +148,7 @@ export default class I18nextPlugin {
147148
}
148149
});
149150
this.context = compiler.options.context || "";
151+
this.initMissingDir();
150152

151153
compiler.plugin("compilation", (compilation, data) => {
152154
// reset for new compliation
@@ -185,6 +187,21 @@ export default class I18nextPlugin {
185187
compiler.plugin("after-emit", this.onAfterEmit.bind(this));
186188
}
187189

190+
protected async initMissingDir() {
191+
if (this.missingDirInitialized) {
192+
return;
193+
}
194+
195+
const template = path.resolve(this.context, this.option.pathToSaveMissing);
196+
await Promise.all(this.option.namespaces.map(ns => this.option.languages.map(async lng => {
197+
const dirPath = path.dirname(getPath(template, lng, ns));
198+
if (!await exists(dirPath)) {
199+
await mkdir(dirPath);
200+
}
201+
})));
202+
this.missingDirInitialized = true;
203+
}
204+
188205
protected async onEmit(compilation: wp.Compilation, callback: (err?: Error) => void) {
189206
// emit translation files
190207
this.prevTimestamps = compilation.fileTimestamps;
@@ -234,6 +251,7 @@ export default class I18nextPlugin {
234251
));
235252
try {
236253
// write missing
254+
await this.initMissingDir();
237255
await Promise.all(_.map(this.missingKeys, async (namespaces, lng) => {
238256
const resourceTemplate = path.join(this.context, getPath(this.option.pathToSaveMissing, lng));
239257
const resourceDir = path.dirname(resourceTemplate);

0 commit comments

Comments
 (0)