Comparing version 1.0.0 to 1.1.0
@@ -8,4 +8,3 @@ export declare enum PreDelete { | ||
DELETED = 0, | ||
PRESERVED = 1, | ||
CREATED = 2 | ||
PRESERVED = 1 | ||
} | ||
@@ -17,7 +16,7 @@ /** | ||
outputPath: string; | ||
knowExist: Map<string, boolean>; | ||
knowExist: Map<string, false | DirOutput>; | ||
knowNoExist: Set<unknown>; | ||
additionalFiles: boolean; | ||
preDelete: Map<string, Promise<PreDelete>>; | ||
preCreate: Map<string, Promise<PreCreate>>; | ||
preCreate: Map<string, Promise<DirOutput | PreCreate>>; | ||
/** | ||
@@ -35,9 +34,9 @@ * @param outputPath The path to the dir. | ||
/** | ||
* Creates a dir. If this dir was scheduled to be deleted by a `empty` operation, this dir is not deleted. | ||
* Creates a dir. If this dir was scheduled to be deleted by a `empty` operation, this dir is not deleted. Returns a DirOutput with the outputPath being the path of the dir that was created. | ||
* @param name {string} The name of the dir. | ||
* @param empty=true {boolean} Whether or not to empty the dir if it was preserved instead of deleted. | ||
* @returns {Promise} | ||
* @fulfil void | ||
* @returns {Promise} | ||
* @fulfil DirOutput | ||
*/ | ||
createDir(name: string, empty?: boolean): Promise<void>; | ||
createDir(name: string, empty?: boolean): Promise<DirOutput>; | ||
/** | ||
@@ -44,0 +43,0 @@ * Remove all files and dirs in the output dir. |
@@ -21,3 +21,2 @@ "use strict"; | ||
PreCreate[PreCreate["PRESERVED"] = 1] = "PRESERVED"; | ||
PreCreate[PreCreate["CREATED"] = 2] = "CREATED"; | ||
})(PreCreate = exports.PreCreate || (exports.PreCreate = {})); | ||
@@ -100,14 +99,15 @@ /** | ||
/** | ||
* Creates a dir. If this dir was scheduled to be deleted by a `empty` operation, this dir is not deleted. | ||
* Creates a dir. If this dir was scheduled to be deleted by a `empty` operation, this dir is not deleted. Returns a DirOutput with the outputPath being the path of the dir that was created. | ||
* @param name {string} The name of the dir. | ||
* @param empty=true {boolean} Whether or not to empty the dir if it was preserved instead of deleted. | ||
* @returns {Promise} | ||
* @fulfil void | ||
* @returns {Promise} | ||
* @fulfil DirOutput | ||
*/ | ||
async createDir(name, empty = true) { | ||
// Check if it already exists as a dir | ||
const preCreate = this.preCreate.get(name); | ||
const knowExist = this.knowExist.get(name); | ||
if (knowExist !== undefined) { | ||
if (knowExist) { | ||
return; | ||
if (knowExist !== undefined && preCreate === undefined) { | ||
if (knowExist instanceof DirOutput) { | ||
return knowExist; | ||
} | ||
@@ -118,3 +118,3 @@ else { | ||
} | ||
const dirPath = path_1.join(this.outputPath, name); | ||
const dirPath = `${this.outputPath}/${name}`; | ||
// Create | ||
@@ -124,25 +124,40 @@ const create = async () => { | ||
this.preDelete.set(name, create.then(() => PreDelete.EXISTS)); | ||
this.preCreate.set(name, create.then(() => PreCreate.CREATED)); | ||
this.preCreate.set(name, create.then(() => new DirOutput(dirPath))); | ||
await create; | ||
this.knowNoExist.delete(name); | ||
this.knowExist.set(name, true); | ||
const dirOutput = new DirOutput(dirPath); | ||
this.knowExist.set(name, dirOutput); | ||
this.preDelete.delete(name); | ||
this.preCreate.delete(name); | ||
return dirOutput; | ||
}; | ||
// Check preCreate | ||
const preCreate = this.preCreate.get(name); | ||
if (preCreate !== undefined) { | ||
const result = await preCreate; | ||
if (result === PreCreate.DELETED) { | ||
await create(); | ||
if (result instanceof DirOutput) { | ||
return result; | ||
} | ||
else if (result === PreCreate.PRESERVED && empty) { | ||
const empty = fs_extra_1.emptyDir(dirPath); | ||
this.preDelete.set(name, empty.then(() => PreDelete.EXISTS)); | ||
await empty; | ||
this.preDelete.delete(name); | ||
else { | ||
if (result === PreCreate.DELETED) { | ||
return await create(); | ||
} | ||
else { | ||
if (empty) { | ||
const empty = fs_extra_1.emptyDir(dirPath); | ||
this.preDelete.set(name, empty.then(() => PreDelete.EXISTS)); | ||
await empty; | ||
this.preDelete.delete(name); | ||
} | ||
const knowExist = this.knowExist.get(name); | ||
if (knowExist instanceof DirOutput) { | ||
return knowExist; | ||
} | ||
else { | ||
throw new Error('knowExist should be an instance of DirOutput, but it isn\'t. This is an internal bug.'); | ||
} | ||
} | ||
} | ||
} | ||
else { | ||
await create(); | ||
return await create(); | ||
} | ||
@@ -162,3 +177,5 @@ } | ||
files.forEach(file => { | ||
this.knowExist.set(file.name, file.isDirectory()); | ||
this.knowExist.set(file.name, file.isDirectory() | ||
? new DirOutput(path_1.join(this.outputPath, file.name)) | ||
: false); | ||
}); | ||
@@ -165,0 +182,0 @@ } |
@@ -17,4 +17,3 @@ /** | ||
DELETED = 0, | ||
PRESERVED = 1, | ||
CREATED = 2 | ||
PRESERVED = 1 | ||
} | ||
@@ -27,7 +26,7 @@ | ||
outputPath: string | ||
knowExist = new Map<string, boolean>() | ||
knowExist = new Map<string, false | DirOutput>() | ||
knowNoExist = new Set() | ||
additionalFiles = true | ||
preDelete = new Map<string, Promise<PreDelete>>() | ||
preCreate = new Map<string, Promise<PreCreate>>() | ||
preCreate = new Map<string, Promise<PreCreate | DirOutput>>() | ||
@@ -98,14 +97,15 @@ /** | ||
/** | ||
* Creates a dir. If this dir was scheduled to be deleted by a `empty` operation, this dir is not deleted. | ||
* Creates a dir. If this dir was scheduled to be deleted by a `empty` operation, this dir is not deleted. Returns a DirOutput with the outputPath being the path of the dir that was created. | ||
* @param name {string} The name of the dir. | ||
* @param empty=true {boolean} Whether or not to empty the dir if it was preserved instead of deleted. | ||
* @returns {Promise} | ||
* @fulfil void | ||
* @returns {Promise} | ||
* @fulfil DirOutput | ||
*/ | ||
async createDir (name: string, empty: boolean = true): Promise<void> { | ||
async createDir (name: string, empty: boolean = true): Promise<DirOutput> { | ||
// Check if it already exists as a dir | ||
const preCreate = this.preCreate.get(name) | ||
const knowExist = this.knowExist.get(name) | ||
if (knowExist !== undefined) { | ||
if (knowExist) { | ||
return | ||
if (knowExist !== undefined && preCreate === undefined) { | ||
if (knowExist instanceof DirOutput) { | ||
return knowExist | ||
} else { | ||
@@ -116,29 +116,42 @@ throw new Error('Could not create dir because it already exists and is a file.') | ||
const dirPath = join(this.outputPath, name) | ||
const dirPath = `${this.outputPath}/${name}` | ||
// Create | ||
const create = async (): Promise<void> => { | ||
const create = async (): Promise<DirOutput> => { | ||
const create = fs.mkdir(dirPath) | ||
this.preDelete.set(name, create.then(() => PreDelete.EXISTS)) | ||
this.preCreate.set(name, create.then(() => PreCreate.CREATED)) | ||
this.preCreate.set(name, create.then(() => new DirOutput(dirPath))) | ||
await create | ||
this.knowNoExist.delete(name) | ||
this.knowExist.set(name, true) | ||
const dirOutput = new DirOutput(dirPath) | ||
this.knowExist.set(name, dirOutput) | ||
this.preDelete.delete(name) | ||
this.preCreate.delete(name) | ||
return dirOutput | ||
} | ||
// Check preCreate | ||
const preCreate = this.preCreate.get(name) | ||
if (preCreate !== undefined) { | ||
const result = await preCreate | ||
if (result === PreCreate.DELETED) { | ||
await create() | ||
} else if (result === PreCreate.PRESERVED && empty) { | ||
const empty = emptyDir(dirPath) | ||
this.preDelete.set(name, empty.then(() => PreDelete.EXISTS)) | ||
await empty | ||
this.preDelete.delete(name) | ||
if (result instanceof DirOutput) { | ||
return result | ||
} else { | ||
if (result === PreCreate.DELETED) { | ||
return await create() | ||
} else { | ||
if (empty) { | ||
const empty = emptyDir(dirPath) | ||
this.preDelete.set(name, empty.then(() => PreDelete.EXISTS)) | ||
await empty | ||
this.preDelete.delete(name) | ||
} | ||
const knowExist = this.knowExist.get(name) | ||
if (knowExist instanceof DirOutput) { | ||
return knowExist | ||
} else { | ||
throw new Error('knowExist should be an instance of DirOutput, but it isn\'t. This is an internal bug.') | ||
} | ||
} | ||
} | ||
} else { | ||
await create() | ||
return await create() | ||
} | ||
@@ -159,3 +172,5 @@ } | ||
files.forEach(file => { | ||
this.knowExist.set(file.name, file.isDirectory()) | ||
this.knowExist.set(file.name, file.isDirectory() | ||
? new DirOutput(join(this.outputPath, file.name)) | ||
: false) | ||
}) | ||
@@ -162,0 +177,0 @@ } |
{ | ||
"name": "dir-output", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "An interface for managing a dir.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -50,6 +50,6 @@ <a name="module_dir-output"></a> | ||
#### dirOutput.createDir(name, empty) ⇒ <code>Promise</code> | ||
Creates a dir. If this dir was scheduled to be deleted by a `empty` operation, this dir is not deleted. | ||
Creates a dir. If this dir was scheduled to be deleted by a `empty` operation, this dir is not deleted. Returns a DirOutput with the outputPath being the path of the dir that was created. | ||
**Kind**: instance method of [<code>DirOutput</code>](#module_dir-output..DirOutput) | ||
**Fulfil**: void | ||
**Fulfil**: DirOutput | ||
@@ -56,0 +56,0 @@ | Param | Type | Default | Description | |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
29786
457