Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Do not merge] Hacky fix to build authorization extension in node18 #64

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
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
214 changes: 111 additions & 103 deletions bin/a0-ext
Original file line number Diff line number Diff line change
@@ -1,72 +1,69 @@
#!/usr/bin/env node

const _ = require('lodash');
const chalk = require('chalk');
const json = require('load-json-file');
const path = require('path');
const prog = require('caporal');
const request = require('superagent');
const Webpack = require('webpack');
const validator = require('webtask-json-validator');
const AdmZip = require('adm-zip');

const getConfig = require('../lib/config');
const getExternals = require('../lib/externals');
const getClientConfig = require('../lib/client-config');
const _ = require("lodash");
const chalk = require("chalk");
const json = require("load-json-file");
const path = require("path");
const prog = require("caporal");
const Webpack = require("webpack");
const validator = require("webtask-json-validator");

const getConfig = require("../lib/config");
const getClientConfig = require("../lib/client-config");
const getExternals = require("../lib/externals");

prog
.version('1.0.0')
.version("1.0.0")
.command(
'build:server',
'Compile your Node.js Backend into an Auth0 Extension'
"build:server",
"Compile your Node.js Backend into an Auth0 Extension"
)
.argument('entryPoint', 'Entry point for your extension')
.argument("entryPoint", "Entry point for your extension")
.argument(
'destinationFolder',
'Folder where to store the output of the build'
"destinationFolder",
"Folder where to store the output of the build"
)
.option('--pkg <pkg>', 'Path of package.json')
.option("--pkg <pkg>", "Path of package.json")
.action((args, opts) => {
const rootPath = process.cwd();
const packagePath = path.join(rootPath, opts.pkg || 'package.json');
const packagePath = path.join(rootPath, opts.pkg || "package.json");

json(packagePath).then((pkg) => {
console.log(
'Building:',
"Building:",
chalk.bold(`${pkg.name}@${pkg.version} extension`)
);

getExternals(rootPath)
.then(externals => getConfig(pkg, rootPath, args, externals))
.then((externals) => getConfig(pkg, rootPath, args, externals))
.then((config) => {
Webpack(config).run((err, stats) => {
if (err) {
console.log('Error:', chalk.bold(JSON.stringify(err, null, 2)));
console.log("Error:", chalk.bold(JSON.stringify(err, null, 2)));
} else {
const chunks = stats.toJson({ chunks: true }).chunks;
console.log(
'Output:',
"Output:",
chalk.bold(
`${path.join(args.destinationFolder, chunks[0].files[0])}`
)
);

const largeModules = _.chain(chunks[0].modules)
.orderBy(['size'], ['desc'])
.filter(m => m.size > 5000)
.orderBy(["size"], ["desc"])
.filter((m) => m.size > 5000)
.value();
if (largeModules && largeModules.length) {
largeModules.forEach(m =>
largeModules.forEach((m) =>
console.log(` - ${m.name} (${Math.round(m.size / 1024)} KB)`)
);
}

console.log('\n');
console.log("\n");
console.log(
stats.toString({
colors: true,
cached: false,
cachedModules: false
cachedModules: false,
})
);
}
Expand All @@ -75,32 +72,35 @@ prog
});
})
.command(
'build:client',
'Compile your React Frontend into an Auth0 Extension Client'
"build:client",
"Compile your React Frontend into an Auth0 Extension Client"
)
.argument('entryPoint', 'Entry point for your extension')
.argument('destinationFolder', 'Entry point for your extension')
.option('--pkg <pkg>', 'Path of package.json')
.argument("entryPoint", "Entry point for your extension")
.argument("destinationFolder", "Entry point for your extension")
.option("--pkg <pkg>", "Path of package.json")
.action((args, opts) => {
console.log("a0 build:client command");

const rootPath = process.cwd();
const entry = args.entryPoint || 'client/app.jsx';
const destination = args.destinationFolder || 'dist';
const mode = 'production';
const packagePath = path.join(rootPath, opts.pkg || 'package.json');
const entry = args.entryPoint || "client/app.jsx";
const destination = args.destinationFolder || "dist";
const mode = "production";
const packagePath = path.join(rootPath, opts.pkg || "package.json");

json(packagePath).then((pkg) => {
console.log('Building:', chalk.bold(`${pkg.name}@${pkg.version} client`));
console.log("Building:", chalk.bold(`${pkg.name}@${pkg.version} client`));

Webpack(getClientConfig(pkg, rootPath, mode, entry, destination)).run(
(err, stats) => {
if (err) {
console.log('Error:', chalk.bold(JSON.stringify(err, null, 2)));
console.log(err);
console.log("Error:", chalk.bold(JSON.stringify(err, null, 2)));
} else {
console.log(
stats.toString({
colors: true,
cached: false,
cachedModules: false
cachedModules: false,
})
);
}
Expand All @@ -109,13 +109,13 @@ prog
});
})
.command(
'validate',
'Check extension`s package.json to contain all necessary information'
"validate",
"Check extension`s package.json to contain all necessary information"
)
.argument('entryPoint', 'Entry point for your extension')
.argument("entryPoint", "Entry point for your extension")
.action((args) => {
const rootPath = process.cwd();
const entry = args.entryPoint || 'package.json';
const entry = args.entryPoint || "package.json";
const packagePath = path.join(rootPath, entry);

json(packagePath).then((pkg) => {
Expand All @@ -132,91 +132,99 @@ prog
return process.exit((result.errors && result.errors.length) || 1);
});
})
.command('package', 'Package assets for extension deployment')
.option('--pkg <pkg>', 'Path of package.json')
.option('--client <client>', 'Path of client side assets')
.option('--bundle <bundle>', 'Path to extensions bundle')
.option('--out <out>', 'Directory to copy build package')
.command("package", "Package assets for extension deployment")
.option("--pkg <pkg>", "Path of package.json")
.option("--client <client>", "Path of client side assets")
.option("--bundle <bundle>", "Path to extensions bundle")
.option("--out <out>", "Directory to copy build package")
.action((args, opts) => {
const rootPath = process.cwd();
const packagePath = path.join(rootPath, opts.pkg || 'package.json');

const packagePath = path.join(rootPath, opts.pkg || "package.json");
json(packagePath).then((pkg) => {
console.log(`Validating ${packagePath}`);
try {
console.log(`Validating ${packagePath}`);

const result = validator.validate(pkg, true);
const result = validator.validate(pkg, true);
if (!result.isValid) {
console.log(`${packagePath} is invalid.`, result.errors);
return process.exit((result.errors && result.errors.length) || 1);
}

if (!result.isValid) {
console.log(`${packagePath} is invalid.`, result.errors);
return process.exit((result.errors && result.errors.length) || 1);
}
const clientPath = opts.client
? path.join(rootPath, opts.client)
: null;
const bundlePath = path.join(
rootPath,
opts.bundle || `./dist/${pkg.name}.extension.${pkg.version}.js`
);

const outPath = path.join(
rootPath,
opts.out || pkg["auth0-extension"].main
);
console.log(
"Packaging:",
chalk.bold(`${pkg.name}@${pkg.version} extension assets`)
);

const zip = new AdmZip();

zip.addLocalFile(packagePath);
zip.addLocalFile(bundlePath, "", "extension.js");

if (clientPath) {
zip.addLocalFolder(
clientPath,
"client",
(filename) => filename.endsWith(".js") || filename.endsWith(".css")
);
}

const clientPath = opts.client ? path.join(rootPath, opts.client) : null;
const bundlePath = path.join(
rootPath,
opts.bundle || `./dist/${pkg.name}.extension.${pkg.version}.js`
);
const outPath = path.join(
rootPath,
opts.out || pkg['auth0-extension'].main
);
const zipName = path.join(outPath, "package.zip");

console.log(
'Packaging:',
chalk.bold(`${pkg.name}@${pkg.version} extension assets`)
);
return zip.writeZip(zipName, (err) => {

const zip = new AdmZip();
if (err) {
console.log("Error:", `Unable to package ${zipName}`);
return process.exit(1);
}

zip.addLocalFile(packagePath);
zip.addLocalFile(bundlePath, '', 'extension.js');
if (clientPath) {
zip.addLocalFolder(clientPath,
'client',
filename => filename.endsWith('.js') || filename.endsWith('.css'));
console.log(`${zipName} packaged successfully`);
return process.exit(0);
});
} catch (error) {
console.log("Error:", error);
}

const zipName = path.join(outPath, 'package.zip');

return zip.writeZip(zipName, (err) => {
if (err) {
console.log('Error:', `Unable to package ${zipName}`);
return process.exit(1);
}

console.log(`${zipName} packaged successfully`);
return process.exit(0);
});
});
})
.command('deploy', 'Deploys the extension to Auth0.')
.option('--apiKey <apiKey>', 'API Key')
.option('--package <package>', 'Path to extension bundle')
.option('--url <url>', 'URL to extension deploy endpoint')
.command("deploy", "Deploys the extension to Auth0.")
.option("--apiKey <apiKey>", "API Key")
.option("--package <package>", "Path to extension bundle")
.option("--url <url>", "URL to extension deploy endpoint")
.action((args, opts) => {
const rootPath = process.cwd();
const filePath = path.join(rootPath, opts.package || 'dist/package.zip');
const url = opts.url || 'http://0.0.0.0:3000/api/extensions';
const filePath = path.join(rootPath, opts.package || "dist/package.zip");
const url = opts.url || "http://0.0.0.0:3000/api/extensions";
const apiKey = opts.apiKey || process.env.AUTH0_EXTENSIONS_API_KEY;

if (!apiKey) {
console.error(
'An API key is required. Please set --apiKey flag or set the environment variable AUTH0_EXTENSIONS_API_KEY'
"An API key is required. Please set --apiKey flag or set the environment variable AUTH0_EXTENSIONS_API_KEY"
);
return process.exit(1);
}

return request
.post(url)
.set({ 'x-api-key': apiKey })
.attach('package', filePath)
.set({ "x-api-key": apiKey })
.attach("package", filePath)
.then(
() => {
console.log('Extension has been deployed successfully');
console.log("Extension has been deployed successfully");
return process.exit(0);
},
(err) => {
console.log('Deploy process failed');
console.log("Deploy process failed");
console.log((err.response && err.response.error) || err);
return process.exit(1);
}
Expand Down
Loading