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

CLI improvements #1611

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
12 changes: 11 additions & 1 deletion cli/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const run = (args) => {

/* webpack set up */
const extensionPath = args.extend ? path.resolve(args.extend) : undefined;
const customOutputPath = utils.customOutputPath(args.extend);
const customOutputPath = getCustomOutputPath(args.extend);
const webpackConfig = generateWebpackConfig({extensionPath, devMode: false, customOutputPath, analyzeBundle: args.analyzeBundle});
const compiler = webpack(webpackConfig);

Expand Down Expand Up @@ -61,6 +61,16 @@ const run = (args) => {

};

/* Where should the built files be saved? (or sourced??)
* This may grow more complex over time
*/
function getCustomOutputPath(extensions) {
if (extensions && path.resolve(__dirname, "..") !== process.cwd()) {
return process.cwd();
}
return false;
}

module.exports = {
addParser,
run
Expand Down
11 changes: 0 additions & 11 deletions cli/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,6 @@ const readFilePromise = (fileName) => {
});
};

/* Where should the built files be saved? (or sourced??)
* This may grow more complex over time
*/
const customOutputPath = (extensions) => {
if (extensions && path.resolve(__dirname, "..") !== process.cwd()) {
return process.cwd();
}
return false;
};

/* write an index.html file to the current working directory
* Optionally set the hrefs for local files to relative links (needed for github pages)
*/
Expand All @@ -108,7 +98,6 @@ module.exports = {
warn,
error,
resolveLocalDirectory,
customOutputPath,
readFilePromise,
exportIndexDotHtml
};
53 changes: 30 additions & 23 deletions cli/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ const addParser = (parser) => {
subparser.addArgument('--handlers', {action: "store", metavar: "JS", help: "Overwrite the provided server handlers for client requests. See documentation for more details."});
subparser.addArgument('--datasetDir', {metavar: "PATH", help: "Directory where datasets (JSONs) are sourced. This is ignored if you define custom handlers."});
subparser.addArgument('--narrativeDir', {metavar: "PATH", help: "Directory where narratives (Markdown files) are sourced. This is ignored if you define custom handlers."});
subparser.addArgument('--customBuildOnly', {action: "storeTrue", help: "Error if a custom build is not found."});
Copy link
Member

@jameshadfield jameshadfield Dec 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the intention here but the word "custom" is a bit overloaded here -- this is checking if an auspice build is present in the current working directory, not whether the build has been customised (which would require introspection of the bundle).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've split the addition of this argument into its own commit: 0090265

Ok I understand the difference now. I chose --customBuildOnly following the previous existence of --customBuild, which seemed to be doing something similar? I can't think of a better name without being verbose like --onlyUseLocalAuspiceBuild. Do you have suggestions?

/* there are some options which we deliberately do not document via `--help`. */
subparser.addArgument('--customBuild', {action: "storeTrue", help: SUPPRESS}); /* see printed warning in the code below */
subparser.addArgument('--gh-pages', {action: "store", help: SUPPRESS}); /* related to the "static-site-generation" or "github-pages" */
};

Expand Down Expand Up @@ -75,28 +75,39 @@ const loadAndAddHandlers = ({app, handlersArg, datasetDir, narrativeDir}) => {
`Looking for datasets in ${datasetsPath}\nLooking for narratives in ${narrativesPath}`;
};

const getAuspiceBuild = () => {
const getAuspiceBuild = (customBuildOnly) => {
const cwd = path.resolve(process.cwd());
const sourceDir = path.resolve(__dirname, "..");
if (
cwd !== sourceDir &&
fs.existsSync(path.join(cwd, "index.html")) &&
fs.existsSync(path.join(cwd, "dist")) &&
fs.readdirSync(path.join(cwd, "dist")).filter((fn) => fn.match(/^auspice.main.bundle.[a-z0-9]+.js$/)).length === 1
) {
return {
message: "Serving the auspice build which exists in this directory.",
baseDir: cwd,
distDir: path.join(cwd, "dist")
};

// Default to current working directory.
let baseDir = cwd;
if (!hasAuspiceBuild(cwd)) {
if (cwd === sourceDir || customBuildOnly) {
utils.error(`Auspice build files not found under ${cwd}. Did you run \`auspice build\` in this directory?`);
process.exit(1);
}
if (!hasAuspiceBuild(sourceDir)) {
utils.error(`Auspice build files not found under ${cwd} or ${sourceDir}. Did you run \`auspice build\` in either directory?`);
process.exit(1);
}
utils.log(`Auspice build files not found under ${cwd}. Using build files under ${sourceDir}.`)
baseDir = sourceDir;
}
return {
message: `Serving auspice version ${version}`,
baseDir: sourceDir,
distDir: path.join(sourceDir, "dist")
message: `Serving the Auspice build in ${baseDir} (version ${version}).`,
baseDir,
distDir: path.join(baseDir, "dist")
};
};

function hasAuspiceBuild(directory) {
return (
fs.existsSync(path.join(directory, "dist")) &&
fs.existsSync(path.join(directory, "dist/index.html")) &&
fs.readdirSync(path.join(directory, "dist")).filter((fn) => fn.match(/^auspice\.main\.bundle\.[a-z0-9]+\.js$/)).length === 1
)
}

const run = (args) => {
/* Basic server set up */
const app = express();
Expand All @@ -105,13 +116,9 @@ const run = (args) => {
app.use(compression());
app.use(nakedRedirect({reverse: true})); /* redirect www.name.org to name.org */

if (args.customBuild) {
utils.warn("--customBuild is no longer used and will be removed in a future version. We now serve a custom auspice build if one exists in the directory `auspice view` is run from");
}

const auspiceBuild = getAuspiceBuild();
utils.verbose(`Serving index / favicon etc from "${auspiceBuild.baseDir}"`);
utils.verbose(`Serving built javascript from "${auspiceBuild.distDir}"`);
const auspiceBuild = getAuspiceBuild(args.customBuildOnly);
utils.verbose(`Serving favicon from "${auspiceBuild.baseDir}"`);
utils.verbose(`Serving index and built javascript from "${auspiceBuild.distDir}"`);
app.get("/favicon.png", (req, res) => {res.sendFile(path.join(auspiceBuild.baseDir, "favicon.png"));});
app.use("/dist", expressStaticGzip(auspiceBuild.distDir, {maxAge: '30d'}));

Expand Down