-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #109 from rocjs/template-as-a-zip
Template as a zip
- Loading branch information
Showing
9 changed files
with
138 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ node_modules | |
lib | ||
esdocs | ||
coverage | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import temp from 'temp'; | ||
import fs from 'fs'; | ||
import nodeUnzip from 'unzip'; | ||
import path from 'path'; | ||
|
||
// Automatically track and cleanup files at exit | ||
temp.track(); | ||
|
||
/** | ||
* Unpacks the given zip file. | ||
* | ||
* @param {string} zipFile - The full path to a local zip file. | ||
* | ||
* @returns {Promise} The path to the temporary directory where the unzipped files are located. | ||
*/ | ||
export default function unzip(zipFile) { | ||
if (!zipFile) { | ||
throw new Error('No zip file was given.'); | ||
} | ||
|
||
return new Promise((resolve, reject) => { | ||
temp.mkdir('roc', (err, dirPath) => { | ||
if (err) { | ||
return reject(err); | ||
} | ||
|
||
fs.createReadStream(zipFile) | ||
.pipe(nodeUnzip.Extract({ path: dirPath })) // eslint-disable-line new-cap | ||
.on('error', reject) | ||
.on('close', () => { | ||
// The template zip is assumed to have a root dir | ||
try { | ||
fs.readdirSync(dirPath) | ||
.forEach((file) => { | ||
const rootDir = path.join(dirPath, file); | ||
if (fs.statSync(rootDir).isDirectory()) { | ||
return resolve(rootDir); | ||
} | ||
}); | ||
} catch (error) { | ||
return reject(error); | ||
} | ||
return resolve(dirPath); | ||
}); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters