Skip to content
This repository has been archived by the owner on Oct 21, 2020. It is now read-only.

Include files listed in manifest.json #20

Merged
merged 4 commits into from
Apr 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
"debug": "^2.6.3",
"denodeify": "^1.2.1",
"meow": "^3.7.0",
"node-dir": "^0.1.16",
"node-rsa": "~0.4.2",
"stream-buffers": "^3.0.1",
"yazl": "^2.4.2"
Expand Down
91 changes: 50 additions & 41 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ const fs = require('fs');
const ZipFile = require('yazl').ZipFile;
const denodeify = require('denodeify');
const writeFile = denodeify(fs.writeFile);
const nodeDir = require('node-dir');
const listPaths = denodeify(nodeDir.paths);
const listFiles = denodeify(nodeDir.files);
const streamBuffers = require('stream-buffers');
const debug = require('debug')('cli');
const validate = require('@teppeis/kintone-plugin-manifest-validator');
Expand Down Expand Up @@ -35,7 +32,9 @@ function cli(pluginDir, options) {
throw new Error('Manifest file $PLUGIN_DIR/manifest.json not found.');
}

const result = validate(loadJson(manifestJsonPath), {
// 3. validate manifest.json
const manifest = loadJson(manifestJsonPath);
const result = validate(manifest, {
relativePath: validateRelativePath(pluginDir),
maxFileSize: validateMaxFileSize(pluginDir),
});
Expand All @@ -53,37 +52,23 @@ function cli(pluginDir, options) {
const outputDir = path.dirname(path.resolve(pluginDir));
debug(`outDir : ${outputDir}`);

return listFiles(pluginDir).then(files => {
files.forEach(file => {
const basename = path.basename(file);
// 3. check dot files
if (/^\./.test(basename)) {
throw new Error(`PLUGIN_DIR must not contain dot files or directories : ${file}`);
}
// 4. check *.ppk
if (/\.ppk$/.test(basename)) {
throw new Error(`PLUGIN_DIR must not contain * .ppk : ${file}`);
// 4. generate new ppk if not specified
const ppkFile = options.ppk;
let privateKey;
if (ppkFile) {
debug(`loading an existing key: ${ppkFile}`);
privateKey = fs.readFileSync(ppkFile, 'utf8');
}

// 5. package plugin.zip
return createContentsZip(pluginDir, manifest)
.then(contentsZip => packerLocal(contentsZip, privateKey))
.then(output => {
if (!ppkFile) {
fs.writeFileSync(path.join(outputDir, `${output.id}.ppk`), output.privateKey, 'utf8');
}
return outputPlugin(outputDir, output.plugin);
});
}).then(() => {
// 5. generate new ppk if not specified
const ppkFile = options.ppk;
let privateKey;
if (ppkFile) {
debug(`loading an existing key: ${ppkFile}`);
privateKey = fs.readFileSync(ppkFile, 'utf8');
}

// 6. package plugin.zip
return createContentsZip(pluginDir)
.then(contentsZip => packerLocal(contentsZip, privateKey))
.then(output => {
if (!ppkFile) {
fs.writeFileSync(path.join(outputDir, `${output.id}.ppk`), output.privateKey, 'utf8');
}
return outputPlugin(outputDir, output.plugin);
});
});
}

module.exports = cli;
Expand All @@ -92,10 +77,11 @@ module.exports = cli;
* Create contents.zip
*
* @param {string} pluginDir
* @param {!Object} manifest
* @return {!Promise<!Buffer>}
*/
function createContentsZip(pluginDir) {
return listPaths(pluginDir).then(result => new Promise((res, rej) => {
function createContentsZip(pluginDir, manifest) {
return new Promise((res, rej) => {
const output = new streamBuffers.WritableStreamBuffer();
const zipFile = new ZipFile();
let size = null;
Expand All @@ -104,16 +90,39 @@ function createContentsZip(pluginDir) {
res(output.getContents());
});
zipFile.outputStream.pipe(output);
result.files.forEach(file => {
zipFile.addFile(file, path.relative(pluginDir, file));
});
result.dirs.forEach(dir => {
zipFile.addEmptyDirectory(path.relative(pluginDir, dir));
createSourceList(manifest).forEach(src => {
zipFile.addFile(path.join(pluginDir, src), src);
});
zipFile.end(finalSize => {
size = finalSize;
});
}));
});
}

/**
* Create content file list from manifest.json
*
* @param {!Object} manifest
* @return {!Array<string>}
*/
function createSourceList(manifest) {
const sourceTypes = [
['desktop', 'js'],
['desktop', 'css'],
['mobile', 'js'],
['config', 'js'],
['config', 'css']
];
const list = sourceTypes
.map(t => manifest[t[0]] && manifest[t[0]][t[1]])
.filter(i => !!i)
.reduce((a, b) => a.concat(b), [])
.filter(file => !/^https?:\/\//.test(file));
if (manifest.config && manifest.config.html) {
list.push(manifest.config.html);
}
list.push('manifest.json', manifest.icon);
return list;
}

/**
Expand Down
32 changes: 29 additions & 3 deletions test/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ const glob = require('glob');
const cli = require('../src/cli');
const fixturesDir = path.join(__dirname, 'fixtures');
const outDir = path.join(fixturesDir, 'sample-plugin');
const pluginDir = path.join(outDir, 'plugin-dir');
const ppkPath = path.join(fixturesDir, 'private.ppk');

const ID = 'aaa';
Expand Down Expand Up @@ -60,6 +59,7 @@ describe('cli', () => {
});

context('without ppk', () => {
const pluginDir = path.join(outDir, 'plugin-dir');
let packer;
let pluginFilePath;
beforeEach(() => {
Expand All @@ -82,7 +82,6 @@ describe('cli', () => {
const zip = new AdmZip(packer.args[0][0]);
const files = zip.getEntries().map(entry => entry.entryName).sort();
assert.deepEqual(files, [
'image/',
'image/icon.png',
'manifest.json',
].sort());
Expand All @@ -94,7 +93,7 @@ describe('cli', () => {
});

it('generates a private key file', () => {
const privateKey = fs.readFileSync(path.join(path.dirname(pluginDir), `${ID}.ppk`), 'utf8');
const privateKey = fs.readFileSync(path.join(outDir, `${ID}.ppk`), 'utf8');
assert(privateKey === PRIVATE_KEY);
});

Expand All @@ -105,6 +104,7 @@ describe('cli', () => {
});

context('with ppk', () => {
const pluginDir = path.join(outDir, 'plugin-dir');
let packer;
beforeEach(() => {
packer = sinon.stub().returns({
Expand All @@ -128,4 +128,30 @@ describe('cli', () => {
assert.deepEqual(ppkFiles, []);
});
});

it('includes files listed in manifest.json only', () => {
const pluginDir = path.join(fixturesDir, 'plugin-full-manifest');
let packer = sinon.stub().returns({
id: ID,
privateKey: PRIVATE_KEY,
plugin: PLUGIN_BUFFER,
});

return rimraf(`${outDir}/*.*(ppk|zip)`)
.then(() => cli(pluginDir, {packerMock_: packer}))
.then(() => {
const zip = new AdmZip(packer.args[0][0]);
const files = zip.getEntries().map(entry => entry.entryName).sort();
assert.deepEqual(files, [
'css/config.css',
'css/desktop.css',
'html/config.html',
'image/icon.png',
'js/config.js',
'js/desktop.js',
'js/mobile.js',
'manifest.json',
].sort());
});
});
});
Empty file.
Empty file.
Empty file.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file.
Empty file.
Empty file.
52 changes: 52 additions & 0 deletions test/fixtures/plugin-full-manifest/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"manifest_version": 1,
"version": 1,
"type": "APP",
"name": {
"ja": "サンプルプラグイン",
"en": "sample plugin",
"zh": "插件的例子"
},
"description": {
"ja": "これはサンプルプラグインです。",
"en": "This is sample plugin.",
"zh": "这是插件的例子"
},
"icon": "image/icon.png",
"homepage_url": {
"ja": "http://jp.example.com",
"en": "http://en.example.com",
"zh": "http://zh.example.com"
},
"desktop": {
"js": [
"js/desktop.js",
"https://example.com/js/desktop.js"
],
"css": [
"https://example.com/css/desktop.css",
"css/desktop.css"
]
},
"mobile": {
"js": [
"js/mobile.js",
"https://example.com/js/mobile.js"
]
},
"config": {
"html": "html/config.html",
"js": [
"https://example.com/js/config.js",
"js/config.js"
],
"css": [
"css/config.css",
"https://example.com/css/config.css"
],
"required_params": [
"Param1",
"Param2"
]
}
}
6 changes: 0 additions & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1833,12 +1833,6 @@ natural-compare@^1.4.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/natural-compare/-/natural-compare-1.4.0.tgz#4abebfeed7541f2c27acfb29bdbbd15c8d5ba4f7"

node-dir@^0.1.16:
version "0.1.16"
resolved "https://registry.yarnpkg.com/node-dir/-/node-dir-0.1.16.tgz#d2ef583aa50b90d93db8cdd26fcea58353957fe4"
dependencies:
minimatch "^3.0.2"

node-rsa@~0.4.2:
version "0.4.2"
resolved "https://registry.yarnpkg.com/node-rsa/-/node-rsa-0.4.2.tgz#d6391729ec16a830ed5a38042b3157d2d5d72530"
Expand Down