collect files during packaging
This commit is contained in:
parent
06e5703e7d
commit
7af4f47d9c
1 changed files with 39 additions and 32 deletions
|
@ -1,17 +1,18 @@
|
|||
import { readFile, createWriteStream } from 'fs';
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import * as _ from 'lodash';
|
||||
import * as yazl from 'yazl';
|
||||
import { Manifest } from './manifest';
|
||||
import { nfcall, Promise, reject, resolve } from 'q';
|
||||
import { nfcall, Promise, reject, resolve, all } from 'q';
|
||||
import * as glob from 'glob';
|
||||
|
||||
const resourcesPath = path.join(path.dirname(__dirname), 'resources');
|
||||
const vsixManifestTemplatePath = path.join(resourcesPath, 'extension.vsixmanifest');
|
||||
|
||||
function readManifest(root: string): Promise<Manifest> {
|
||||
const manifestPath = path.join(root, 'package.json');
|
||||
function readManifest(cwd: string): Promise<Manifest> {
|
||||
const manifestPath = path.join(cwd, 'package.json');
|
||||
|
||||
return nfcall<string>(readFile, manifestPath, 'utf8')
|
||||
return nfcall<string>(fs.readFile, manifestPath, 'utf8')
|
||||
.catch(() => reject<string>(`Extension manifest not found: ${ manifestPath }`))
|
||||
.then<Manifest>(manifestStr => {
|
||||
try {
|
||||
|
@ -47,7 +48,7 @@ function validateManifest(manifest: Manifest): Promise<Manifest> {
|
|||
}
|
||||
|
||||
function toVsixManifest(manifest: Manifest): Promise<string> {
|
||||
return nfcall<string>(readFile, vsixManifestTemplatePath, 'utf8')
|
||||
return nfcall<string>(fs.readFile, vsixManifestTemplatePath, 'utf8')
|
||||
.then(vsixManifestTemplateStr => _.template(vsixManifestTemplateStr))
|
||||
.then(vsixManifestTemplate => vsixManifestTemplate({
|
||||
id: manifest.name,
|
||||
|
@ -59,33 +60,39 @@ function toVsixManifest(manifest: Manifest): Promise<string> {
|
|||
}));
|
||||
}
|
||||
|
||||
function writeVsix(packagePath: string, vsixManifest: string): Promise<void> {
|
||||
return Promise<void>((c, e) => {
|
||||
const zip = new yazl.ZipFile();
|
||||
zip.addBuffer(new Buffer(vsixManifest, 'utf8'), 'extension.vsixmanifest');
|
||||
zip.addFile(path.join(resourcesPath, '[Content_Types].xml'), '[Content_Types].xml');
|
||||
zip.addBuffer(new Buffer('hello world', 'utf8'), 'hello.txt');
|
||||
zip.end();
|
||||
|
||||
const zipStream = createWriteStream(packagePath);
|
||||
zip.outputStream.pipe(zipStream);
|
||||
zip.outputStream.once('error', e);
|
||||
zip.outputStream.once('end', c);
|
||||
});
|
||||
function collectFiles(cwd: string): Promise<string[]> {
|
||||
return nfcall<string[]>(glob, '**', { cwd, nodir: true });
|
||||
}
|
||||
|
||||
function defaultPackagePath(root: string, manifest: Manifest) {
|
||||
return path.join(root, `${ manifest.name }-${ manifest.version }.vsix`);
|
||||
}
|
||||
function writeVsix(cwd: string, manifest: Manifest, packagePath: string): Promise<string> {
|
||||
packagePath = packagePath || defaultPackagePath(cwd, manifest);
|
||||
|
||||
export = function (packagePath?: string, root = process.cwd()): Promise<any> {
|
||||
return readManifest(root)
|
||||
.then(validateManifest)
|
||||
.then(manifest => {
|
||||
packagePath = packagePath || defaultPackagePath(root, manifest);
|
||||
return nfcall(fs.unlink, packagePath)
|
||||
.catch(err => err.code !== 'ENOENT' ? reject(err) : resolve(null))
|
||||
.then(() => {
|
||||
return all<any>([toVsixManifest(manifest), collectFiles(cwd)])
|
||||
.spread((vsixManifest: string, files: string[]) => Promise<string>((c, e) => {
|
||||
const zip = new yazl.ZipFile();
|
||||
zip.addBuffer(new Buffer(vsixManifest, 'utf8'), 'extension.vsixmanifest');
|
||||
zip.addFile(path.join(resourcesPath, '[Content_Types].xml'), '[Content_Types].xml');
|
||||
files.forEach(file => zip.addFile(path.join(cwd, file), 'extension/' + file));
|
||||
zip.end();
|
||||
|
||||
return toVsixManifest(manifest)
|
||||
.then(vsixManifest => writeVsix(packagePath, vsixManifest))
|
||||
.then(() => console.log(`Package created: ${ path.resolve(packagePath) }`));
|
||||
const zipStream = fs.createWriteStream(packagePath);
|
||||
zip.outputStream.pipe(zipStream);
|
||||
zip.outputStream.once('error', e);
|
||||
zip.outputStream.once('end', () => c(packagePath));
|
||||
}));
|
||||
});
|
||||
}
|
||||
|
||||
function defaultPackagePath(cwd: string, manifest: Manifest): string {
|
||||
return path.join(cwd, `${ manifest.name }-${ manifest.version }.vsix`);
|
||||
}
|
||||
|
||||
export = function (packagePath?: string, cwd = process.cwd()): Promise<any> {
|
||||
return readManifest(cwd)
|
||||
.then(validateManifest)
|
||||
.then(manifest => writeVsix(cwd, manifest, packagePath))
|
||||
.then(packagePath => console.log(`Package created: ${ packagePath }`));
|
||||
};
|
Loading…
Add table
Reference in a new issue