Skip to content

Commit

Permalink
feat(package): move to esm
Browse files Browse the repository at this point in the history
  • Loading branch information
Iam1337 committed Jan 18, 2023
1 parent d68136a commit d0c9ffd
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 43 deletions.
12 changes: 5 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const verifyPackage = require("./lib/verify");
const preparePackage = require("./lib/prepare");
import verifyPackage from "./lib/verify.js";
import preparePackage from "./lib/prepare.js";

let verified;

async function verifyConditions(pluginConfig, context) {
export async function verifyConditions(pluginConfig, context) {
const { options } = context;
if (options.prepare) {
const preparePlugin = castArray(options.prepare).find(config => config.path && config.path === "@iam1337/create-unitypackage") || {};
Expand All @@ -17,13 +17,11 @@ async function verifyConditions(pluginConfig, context) {
verified = true;
}

async function prepare(pluginConfig, context) {
export async function prepare(pluginConfig, context) {
if (!verified) {
verifyPackage(pluginConfig, context);
verified = true;
}

preparePackage(pluginConfig, context);
}

module.exports = { verifyConditions, prepare };
}
25 changes: 16 additions & 9 deletions lib/definitions/errors.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
const pkg = require("../../package.json");
import { createRequire } from "node:module";
const require = createRequire(import.meta.url);

const pkg = require("../../package.json");
const [homepage] = pkg.homepage.split("#");
const linkify = (file) => `${homepage}/blob/master/${file}`;

module.exports = {
EINVALIDPACKAGEROOT: ({ packageRoot }) => ({
export function EINVALIDPACKAGEROOT ({ packageRoot }) {
return {
message: "Invalid `packageRoot` option.",
details: `The [packageRoot option](${linkify('README.md#packageRoot')}) option, must be a non empty \`String\`.
Your configuration for the \`packageRoot\` option is \`${packageRoot}\`.`
}),
}
}

EINVALIDPROJECTEROOT: ({ projectRoot }) => ({
export function EINVALIDPROJECTEROOT ({ projectRoot })
{
return {
message: "Invalid `projectRoot` option.",
details: `The [projectRoot option](${linkify('README.md#projectRoot')}) option, if defined must be a non empty \`String\`.
Your configuration for the \`projectRoot\` option is \`${projectRoot}\`.`
}),
}
}

EINVALIDOUTPUT: ({ output }) => ({
export function EINVALIDOUTPUT ({ output }) {
return {
message: "Invalid `output` option.",
details: `The [output option](${linkify('README.md#output')}) option, must be a non empty \`String\` and must end with \`.unitypackage\`.
Your configuration for the \`output\` option is \`${output}\`.`
}),
};
}
}
8 changes: 4 additions & 4 deletions lib/get-error.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const SemanticReleaseError = require('@semantic-release/error');
const ERROR_DEFINITIONS = require('./definitions/errors');
import SemanticReleaseError from "@semantic-release/error";
import * as ERROR_DEFINITIONS from "./definitions/errors.js";

module.exports = (code, ctx = {}, context) => {
export default function getError(code, ctx = {}, context) {
const { message, details } = ERROR_DEFINITIONS[code](ctx);
return new SemanticReleaseError(message, code, details);
};
}
10 changes: 5 additions & 5 deletions lib/prepare.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const glob = require("glob");
import glob from "glob";
import createUnityPackage from "unitypackage";

module.exports = ({packageRoot, projectRoot, output}, context) => {
export default function preparePackage({packageRoot, projectRoot, output}, context)
{
const { logger } = context;

const metaFiles = [`${packageRoot}.meta`];
glob.sync(`${packageRoot}/**/*.meta`, {}).map(file => {
metaFiles.push(file);
});

let createUnityPackage = import("unitypackage").default;
logger.log("T: " + createUnityPackage);
createUnityPackage(metaFiles, projectRoot, output, logger.log);
};
}
14 changes: 8 additions & 6 deletions lib/resolve-config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const { isNil } = require("lodash");
import {isNil} from "lodash-es";

module.exports = ({ packageRoot, projectRoot, output }) => ({
packageRoot,
projectRoot: isNil(projectRoot) ? "./" : projectRoot,
output
});
export default function resolveConfig({ packageRoot, projectRoot, output }) {
return {
packageRoot,
projectRoot: isNil(projectRoot) ? "./" : projectRoot,
output
}
}
14 changes: 7 additions & 7 deletions lib/verify.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { isString, isNil } = require("lodash");
const AggregateError = require("aggregate-error");
const getError = require("./get-error");
const resolveConfig = require("./resolve-config");
import {isNil, isString} from "lodash-es";
import AggregateError from "aggregate-error";
import getError from "./get-error.js";
import resolveConfig from "./resolve-config.js";

const isNonEmptyString = (value) => !isNil(value) && isString(value) && value.trim();
const isEndWithUnitypackage = (value) => isNonEmptyString(value) && value.endsWith(".unitypackage");
Expand All @@ -12,16 +12,16 @@ const VALIDATORS = {
output: isEndWithUnitypackage
};

module.exports = (pluginConfig, context) => {
export default function preparePackage(pluginConfig, context) {
const options = resolveConfig(pluginConfig);
const errors = Object.entries(options).reduce(
(errors, [option, value]) => !VALIDATORS[option](value)
? [...errors, getError(`EINVALID${option.toUpperCase()}`, { [option]: value }, context)]
? [...errors, getError(`EINVALID${option.toUpperCase()}`, {[option]: value}, context)]
: errors,
[]
);

if (errors.length > 0) {
throw new AggregateError(errors);
}
};
}
23 changes: 20 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"type": "git",
"url": "git+https://github.com/Iam1337/create-unitypackage.git"
},
"type": "module",
"license": "MIT",
"author": "dr. ext",
"keywords": [
Expand All @@ -28,8 +29,9 @@
"@semantic-release/error": "^3.0.0",
"aggregate-error": "^3.0.0",
"glob": "^7.1.0",
"lodash": "^4.17.21",
"unitypackage": "^1.0.7"
"lodash-es": "^4.17.21",
"unitypackage": "^1.0.7",
"json": "^11.0.0"
},
"peerDependencies": {
"semantic-release": ">=19.0.0"
Expand Down

0 comments on commit d0c9ffd

Please sign in to comment.