Skip to content

Commit

Permalink
setup eslint and prettier. AdobeXD#25
Browse files Browse the repository at this point in the history
  • Loading branch information
yoshikinoko committed Sep 26, 2019
1 parent 90dd2fa commit a9ef8dc
Show file tree
Hide file tree
Showing 17 changed files with 2,589 additions and 1,732 deletions.
11 changes: 11 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
root = true

[*]
indent_style = space
indent_size = 4
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
end_of_line = lf
# editorconfig-tools is unable to ignore longs strings or urls
max_line_length = null
5 changes: 5 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.git/
node_modules/
docs/
build/
package-lock.json
13 changes: 13 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"extends": ["prettier", "plugin:prettier/recommended"],
"plugins": ["prettier"],
"parserOptions": {
"ecmaVersion": 2017
},
"env": {
"browser": false,
"node": true
},
"globals": {},
"rules": {}
}
18 changes: 18 additions & 0 deletions .lintstagedrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"commands/**/*.{js,jsx}": [
"eslint --fix",
"git add"
],
"lib/**/*.{js,jsx}": [
"eslint --fix",
"git add"
],
"__tests__/**/*.json": [
"prettier --write",
"git add"
],
".eslintrc": [
"prettier --write",
"git add"
]
}
24 changes: 14 additions & 10 deletions commands/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,16 @@ function install(opts, args) {
const metadata = getPluginMetadata(sourcePath);
if (!metadata) {
return Object.assign({}, result, {
"error": "Can't install a plugin that doesn't have a valid manifest.json"
error:
"Can't install a plugin that doesn't have a valid manifest.json"
});
}

const id = metadata.id;
if (!id) {
return Object.assign({}, result, {
"error": "Can't install a plugin without a plugin ID in the manifest"
error:
"Can't install a plugin without a plugin ID in the manifest"
});
}

Expand All @@ -63,7 +65,7 @@ function install(opts, args) {
if (fs.existsSync(targetFolder)) {
if (!opts.overwrite) {
return Object.assign({}, result, {
"error": "Plugin exists already; use -o to overwrite"
error: "Plugin exists already; use -o to overwrite"
});
}
if (opts.clean) {
Expand All @@ -78,11 +80,13 @@ function install(opts, args) {
// but this is the gist of what we're trying to accomplish
// shell.cp("-R", path.join(sourcePath, "*"), targetFolder)

const files = ignoreWalk.sync({
path: sourcePath,
ignoreFiles: [".gitignore", ".xdignore", ".npmignore"],
includeEmpty: false,
}).filter(filterAlwaysIgnoredFile);
const files = ignoreWalk
.sync({
path: sourcePath,
ignoreFiles: [".gitignore", ".xdignore", ".npmignore"],
includeEmpty: false
})
.filter(filterAlwaysIgnoredFile);

files.forEach(file => {
const srcFile = path.join(sourcePath, file);
Expand All @@ -95,7 +99,7 @@ function install(opts, args) {
});

return Object.assign({}, result, {
"ok": `"${metadata.name}"@${metadata.version} [${metadata.id}] installed successfully.`
ok: `"${metadata.name}"@${metadata.version} [${metadata.id}] installed successfully.`
});
});

Expand All @@ -114,4 +118,4 @@ function install(opts, args) {
return results;
}

module.exports = install;
module.exports = install;
28 changes: 20 additions & 8 deletions commands/ls.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const path = require("path");
const localXdPath = require("../lib/localXdPath");
const getPluginMetadata = require("../lib/getPluginMetadata");

function ls (opts, args) {
function ls(opts, args) {
const folder = localXdPath(opts);
if (!folder) {
console.fatal(`Could not determine Adobe XD folder.`);
Expand All @@ -30,23 +30,35 @@ function ls (opts, args) {
const plugins = folders.filter(pluginPath => {
const base = path.basename(pluginPath);
const metadata = getPluginMetadata(pluginPath);
if (args.length > 0 && metadata && (!args.includes(base) && !args.includes(metadata.id))) {
if (
args.length > 0 &&
metadata &&
(!args.includes(base) && !args.includes(metadata.id))
) {
return false;
}
if (metadata && !opts.json) {
cli.output(`${base}: "${metadata.name}"@${metadata.version} [${metadata.id}]`);
cli.output(
`${base}: "${metadata.name}"@${metadata.version} [${metadata.id}]`
);
}
return !!metadata;
});
if (opts.json) {
cli.output(JSON.stringify(plugins.map(pluginPath => ({
path: pluginPath,
metadata: getPluginMetadata(pluginPath)
})), null, 2));
cli.output(
JSON.stringify(
plugins.map(pluginPath => ({
path: pluginPath,
metadata: getPluginMetadata(pluginPath)
})),
null,
2
)
);
}
if (plugins.length === 0) {
cli.error(`No valid plugins installed.`);
}
}

module.exports = ls;
module.exports = ls;
38 changes: 23 additions & 15 deletions commands/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,10 @@ const yazl = require("yazl");
const ignoreWalk = require("ignore-walk");
const filterAlwaysIgnoredFile = require("../lib/filterAlwaysIgnoredFile");


/**
* Packages one or more plugins
*/
function package(opts, args) {

if (args.length === 0) {
args.push("."); // assume we want to package the plugin in the cwd
}
Expand All @@ -45,42 +43,52 @@ function package(opts, args) {
const metadata = getPluginMetadata(sourcePath);
if (!metadata) {
return Object.assign({}, result, {
"error": "Can't package a plugin that doesn't have a valid manifest.json"
error:
"Can't package a plugin that doesn't have a valid manifest.json"
});
}

const errors = validate(metadata, {root: sourcePath});
const errors = validate(metadata, { root: sourcePath });
if (errors.length > 0) {
return Object.assign({}, result, {
"error": "Can't package a plugin that has validation errors in the manifest.json:\n" + errors.join("\n")
error:
"Can't package a plugin that has validation errors in the manifest.json:\n" +
errors.join("\n")
});
}

const id = metadata.id;
if (!id) {
return Object.assign({}, result, {
"error": "Can't package a plugin without a plugin ID in the manifest"
error:
"Can't package a plugin without a plugin ID in the manifest"
});
}

result.targetZip = path.join(sourcePath, '..', path.basename(sourcePath) + ".xdx");
result.targetZip = path.join(
sourcePath,
"..",
path.basename(sourcePath) + ".xdx"
);

const zipfile = new yazl.ZipFile();

zipfile.outputStream.pipe(fs.createWriteStream(result.targetZip)).on("close", function() {
});
zipfile.outputStream
.pipe(fs.createWriteStream(result.targetZip))
.on("close", function() {});

const files = ignoreWalk.sync({
path: sourcePath,
ignoreFiles: [".gitignore", ".xdignore", ".npmignore"],
includeEmpty: false,
}).filter(filterAlwaysIgnoredFile);
const files = ignoreWalk
.sync({
path: sourcePath,
ignoreFiles: [".gitignore", ".xdignore", ".npmignore"],
includeEmpty: false
})
.filter(filterAlwaysIgnoredFile);

files.forEach(file => {
zipfile.addFile(path.join(sourcePath, file), file);
});


zipfile.end();

result.ok = `"${metadata.name}"@${metadata.version} [${metadata.id}] packaged successfully at ${result.targetZip}`;
Expand Down
10 changes: 6 additions & 4 deletions commands/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,16 @@ function validatePlugin(opts, args) {
const metadata = getPluginMetadata(sourcePath);
if (!metadata) {
return Object.assign({}, result, {
"error": `Plugin ${pluginToValidate} doesn't have a manifest.`
error: `Plugin ${pluginToValidate} doesn't have a manifest.`
});
}

const errors = validate(metadata, {root: sourcePath});
const errors = validate(metadata, { root: sourcePath });
if (errors.length > 0) {
return Object.assign({}, result, {
"error": `Plugin ${pluginToValidate} has validation errors in the manifest.json:\n` + errors.join("\n")
error:
`Plugin ${pluginToValidate} has validation errors in the manifest.json:\n` +
errors.join("\n")
});
}

Expand All @@ -67,4 +69,4 @@ function validatePlugin(opts, args) {
return results;
}

module.exports = validatePlugin;
module.exports = validatePlugin;
29 changes: 18 additions & 11 deletions commands/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const install = require("./install");
/**
* Watches for changes in one or more plugins and re-installs them automatically
*/
function watch (opts, args) {
function watch(opts, args) {
const folder = localXdPath(opts);
if (!folder) {
console.fatal(`Could not determine Adobe XD folder.`);
Expand All @@ -39,7 +39,9 @@ function watch (opts, args) {

if (opts.json) {
// this doesn't make sense!
cli.output(JSON.stringify({"error": "Can't use JSON output on watch."}));
cli.output(
JSON.stringify({ error: "Can't use JSON output on watch." })
);
return;
}

Expand All @@ -54,14 +56,16 @@ function watch (opts, args) {
const metadata = getPluginMetadata(sourcePath);
if (!metadata) {
return Object.assign({}, result, {
"error": "Can't watch a plugin that doesn't have a valid manifest.json"
error:
"Can't watch a plugin that doesn't have a valid manifest.json"
});
}

const id = metadata.id;
if (!id) {
return Object.assign({}, result, {
"error": "Can't watch a plugin without a plugin ID in the manifest"
error:
"Can't watch a plugin without a plugin ID in the manifest"
});
}

Expand All @@ -71,13 +75,16 @@ function watch (opts, args) {
persistent: true
});

watcher.on("all", debounce(() => {
cli.info(`${metadata.name} changed; reinstalling...`);
install(opts, [ pluginToWatch ]); // only want to reinstall the changed plugin
}, 250));
watcher.on(
"all",
debounce(() => {
cli.info(`${metadata.name} changed; reinstalling...`);
install(opts, [pluginToWatch]); // only want to reinstall the changed plugin
}, 250)
);

return Object.assign({}, result, {
"ok": `Watching ${metadata.name}...`
ok: `Watching ${metadata.name}...`
});
});

Expand All @@ -89,7 +96,7 @@ function watch (opts, args) {
}
});

cli.info(`Watching... press BREAK (CTRL+C) to exit.`)
cli.info(`Watching... press BREAK (CTRL+C) to exit.`);
}

module.exports = watch;
module.exports = watch;
Loading

0 comments on commit a9ef8dc

Please sign in to comment.