From c6d9aabfd0be54afb601cd59542b4a482aaec82f Mon Sep 17 00:00:00 2001 From: Joao Moreno Date: Thu, 5 Nov 2015 23:25:12 -0800 Subject: [PATCH] flow files through. fixes #22 --- src/package.ts | 55 ++++++++++++++++++++++------------------ src/test/package.test.ts | 36 +++++++++++++++++--------- 2 files changed, 54 insertions(+), 37 deletions(-) diff --git a/src/package.ts b/src/package.ts index b781531..8010e59 100644 --- a/src/package.ts +++ b/src/package.ts @@ -262,23 +262,10 @@ export function readManifest(cwd: string): Promise { }); } -export function toVsixManifest(manifest: Manifest, files: IFile[], options: IPackageOptions = {}): Promise { - const processors: IProcessor[] = [ - new MainProcessor(manifest), - new ReadmeProcessor(manifest, options), - new LicenseProcessor(manifest), - new IconProcessor(manifest) - ]; - - return Promise.all(files.map(file => util.chain(file, processors, (file, processor) => processor.onFile(file)))) - .then(files => { - const assets = _.flatten(processors.map(p => p.assets)); - const vsix = ( _.assign)({ assets }, ...processors.map(p => p.vsix)); - +export function toVsixManifest(assets: IAsset[], vsix: any, options: IPackageOptions = {}): Promise { return readFile(vsixManifestTemplatePath, 'utf8') .then(vsixManifestTemplateStr => _.template(vsixManifestTemplateStr)) .then(vsixManifestTemplate => vsixManifestTemplate(vsix)); - }); } export function toContentTypes(files: IFile[]): Promise { @@ -319,20 +306,38 @@ function collectFiles(cwd: string, manifest: Manifest): Promise { }); } +export function processFiles(processors: IProcessor[], files: IFile[], options: IPackageOptions = {}): Promise { + return Promise.all(files.map(file => util.chain(file, processors, (file, processor) => processor.onFile(file)))).then(files => { + const assets = _.flatten(processors.map(p => p.assets)); + const vsix = ( _.assign)({ assets }, ...processors.map(p => p.vsix)); + + return Promise.all([toVsixManifest(assets, vsix, options), toContentTypes(files)]).then(result => { + return [ + { path: 'extension.vsixmanifest', contents: new Buffer(result[0], 'utf8') }, + { path: '[Content_Types].xml', contents: new Buffer(result[1], 'utf8') }, + ...files + ]; + }); + }); +} + +export function createDefaultProcessors(manifest: Manifest, options: IPackageOptions = {}): IProcessor[] { + return [ + new MainProcessor(manifest), + new ReadmeProcessor(manifest, options), + new LicenseProcessor(manifest), + new IconProcessor(manifest) + ]; +} + export function collect(manifest: Manifest, options: IPackageOptions = {}): Promise { const cwd = options.cwd || process.cwd(); + const processors = createDefaultProcessors(manifest, options); return collectFiles(cwd, manifest).then(fileNames => { - const files:IFile[] = fileNames.map(f => ({ path: `extension/${ f }`, localPath: path.join(cwd, f) })); - - return Promise.all([toVsixManifest(manifest, files, options), toContentTypes(files)]) - .then(result => { - return [ - { path: 'extension.vsixmanifest', contents: new Buffer(result[0], 'utf8') }, - { path: '[Content_Types].xml', contents: new Buffer(result[1], 'utf8') }, - ...files - ]; - }); + const files = fileNames.map(f => ({ path: `extension/${ f }`, localPath: path.join(cwd, f) })); + + return processFiles(processors, files, options); }); } @@ -378,7 +383,7 @@ export function pack(options: IPackageOptions = {}): Promise { return readManifest(cwd) .then(manifest => prepublish(cwd, manifest)) - .then(manifest => collect(manifest, options) + .then(manifest => collect(manifest) .then(files => writeVsix(files, path.resolve(options.packagePath || defaultPackagePath(cwd, manifest))) .then(packagePath => ({ manifest, packagePath })))); } diff --git a/src/test/package.test.ts b/src/test/package.test.ts index e4fca07..f28bbd3 100644 --- a/src/test/package.test.ts +++ b/src/test/package.test.ts @@ -1,15 +1,27 @@ -import { readManifest, collect, toVsixManifest, toContentTypes, ReadmeProcessor, read } from '../package'; +import { readManifest, collect, toContentTypes, ReadmeProcessor, read, processFiles, createDefaultProcessors, toVsixManifest, IFile } from '../package'; +import { Manifest } from '../manifest'; import * as path from 'path'; import * as fs from 'fs'; import * as assert from 'assert'; import { parseString } from 'xml2js'; import * as denodeify from 'denodeify'; import * as util from '../util'; +import * as _ from 'lodash'; const readFile = denodeify(fs.readFile); const parseXml = denodeify(parseString); const fixture = name => path.join(__dirname, 'fixtures', name); +function _toVsixManifest(manifest: Manifest, files: IFile[]): Promise { + const processors = createDefaultProcessors(manifest); + return processFiles(processors, files).then(() => { + const assets = _.flatten(processors.map(p => p.assets)); + const vsix = ( _.assign)({ assets }, ...processors.map(p => p.vsix)); + + return toVsixManifest(assets, vsix); + }); +} + describe('collect', () => { it('should catch all files', () => { @@ -63,7 +75,7 @@ describe('toVsixManifest', () => { engines: Object.create(null) }; - return toVsixManifest(manifest, []) + return _toVsixManifest(manifest, []) .then(xml => parseXml(xml)) .then(result => { assert.ok(result); @@ -105,7 +117,7 @@ describe('toVsixManifest', () => { { path: 'extension/readme.md' } ]; - return toVsixManifest(manifest, files) + return _toVsixManifest(manifest, files) .then(xml => parseXml(xml)) .then(result => { assert.equal(result.PackageManifest.Assets[0].Asset.length, 2); @@ -123,7 +135,7 @@ describe('toVsixManifest', () => { engines: Object.create(null) }; - return toVsixManifest(manifest, []) + return _toVsixManifest(manifest, []) .then(xml => parseXml(xml)) .then(result => { assert.equal(result.PackageManifest.Metadata[0].Identity[0].$.Id, 'test'); @@ -145,7 +157,7 @@ describe('toVsixManifest', () => { { path: 'extension/thelicense.md' } ]; - return toVsixManifest(manifest, files) + return _toVsixManifest(manifest, files) .then(xml => parseXml(xml)) .then(result => { assert.equal(result.PackageManifest.Assets[0].Asset.length, 2); @@ -168,7 +180,7 @@ describe('toVsixManifest', () => { { path: 'extension/thelicense.md' } ]; - return toVsixManifest(manifest, files) + return _toVsixManifest(manifest, files) .then(xml => parseXml(xml)) .then(result => { assert.ok(result.PackageManifest.Metadata[0].License); @@ -193,7 +205,7 @@ describe('toVsixManifest', () => { { path: 'extension/thelicense.md' } ]; - return toVsixManifest(manifest, files) + return _toVsixManifest(manifest, files) .then(xml => parseXml(xml)) .then(result => { assert.ok(result.PackageManifest.Metadata[0].Icon); @@ -217,7 +229,7 @@ describe('toVsixManifest', () => { { path: 'extension/fake.png' } ]; - return toVsixManifest(manifest, files) + return _toVsixManifest(manifest, files) .then(xml => parseXml(xml)) .then(result => { assert.ok(result.PackageManifest.Assets[0].Asset.some(d => d.$.Type === 'Microsoft.VisualStudio.Services.Icons.Default' && d.$.Path === 'extension/fake.png')); @@ -240,7 +252,7 @@ describe('toVsixManifest', () => { { path: 'extension\\thelicense.md' } ]; - return toVsixManifest(manifest, files) + return _toVsixManifest(manifest, files) .then(xml => parseXml(xml)) .then(result => { assert.ok(result.PackageManifest.Metadata[0].Icon); @@ -262,7 +274,7 @@ describe('toVsixManifest', () => { } }; - return toVsixManifest(manifest, []) + return _toVsixManifest(manifest, []) .then(xml => parseXml(xml)) .then(result => { const properties = result.PackageManifest.Metadata[0].Properties[0].Property.map(p => p.$); @@ -287,7 +299,7 @@ describe('toVsixManifest', () => { homepage: "https://github.com/Microsoft/vscode-spell-check", }; - return toVsixManifest(manifest, []) + return _toVsixManifest(manifest, []) .then(xml => parseXml(xml)) .then(result => { const properties = result.PackageManifest.Metadata[0].Properties[0].Property.map(p => p.$); @@ -308,7 +320,7 @@ describe('toVsixManifest', () => { categories: ['hello', 'world'] }; - return toVsixManifest(manifest, []) + return _toVsixManifest(manifest, []) .then(xml => parseXml(xml)) .then(result => { const categories = result.PackageManifest.Metadata[0].Categories[0].split(',');