Skip to content

Commit

Permalink
Merge pull request #15 from CloudCannon/more-build-suggestions
Browse files Browse the repository at this point in the history
More build suggestions
  • Loading branch information
rphillips-cc authored Sep 4, 2024
2 parents bff9ed9 + 6779988 commit 22dfaed
Show file tree
Hide file tree
Showing 13 changed files with 421 additions and 17 deletions.
29 changes: 29 additions & 0 deletions src/ssgs/astro.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import Ssg from './ssg.js';

export default class Astro extends Ssg {
constructor() {
super('astro');
}

/**
* Generates a list of build suggestions.
*
* @param filePaths {string[]} List of input file paths.
* @param options {{ config?: Record<string, any>; source?: string; readFile?: (path: string) => Promise<string | undefined>; }}
* @returns {Promise<import('../types').BuildCommands>}
*/
async generateBuildCommands(filePaths, options) {
const commands = await super.generateBuildCommands(filePaths, options);

commands.build.push({
value: 'npx astro build',
attribution: 'default for Astro sites',
});
commands.output.push({
value: 'dist',
attribution: 'default for Astro sites',
});

return commands;
}
}
40 changes: 40 additions & 0 deletions src/ssgs/bridgetown.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { joinPaths } from '../utility.js';
import Ssg from './ssg.js';

export default class Bridgetown extends Ssg {
Expand All @@ -12,4 +13,43 @@ export default class Bridgetown extends Ssg {
templateExtensions() {
return super.templateExtensions().concat(['.liquid']);
}

/**
* Generates a list of build suggestions.
*
* @param filePaths {string[]} List of input file paths.
* @param options {{ config?: Record<string, any>; source?: string; readFile?: (path: string) => Promise<string | undefined>; }}
* @returns {Promise<import('../types').BuildCommands>}
*/
async generateBuildCommands(filePaths, options) {
const commands = await super.generateBuildCommands(filePaths, options);

if (filePaths.includes(joinPaths([options.source, 'Gemfile']))) {
commands.install.unshift({
value: 'bundle install',
attribution: 'because of your Gemfile',
});

if (options.source) {
commands.environment['BUNDLE_GEMFILE'] = {
value: joinPaths([options.source, 'Gemfile']),
attribution: 'because of your Gemfile',
};
}
}

if (filePaths.includes('bin/bridgetown')) {
commands.build.unshift({
value: 'bin/bridgetown deploy',
attribution: 'most common for Bridgetown sites',
});
}

commands.output.unshift({
value: 'output',
attribution: 'most common for Bridgetown sites',
});

return commands;
}
}
29 changes: 29 additions & 0 deletions src/ssgs/gatsby.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import Ssg from './ssg.js';

export default class Gatsby extends Ssg {
constructor() {
super('gatsby');
}

/**
* Generates a list of build suggestions.
*
* @param filePaths {string[]} List of input file paths.
* @param options {{ config?: Record<string, any>; source?: string; readFile?: (path: string) => Promise<string | undefined>; }}
* @returns {Promise<import('../types').BuildCommands>}
*/
async generateBuildCommands(filePaths, options) {
const commands = await super.generateBuildCommands(filePaths, options);

commands.build.unshift({
value: 'npx gatsby build',
attribution: 'default for Gatsby sites',
});
commands.output.unshift({
value: 'public',
attribution: 'default for Gatsby sites',
});

return commands;
}
}
7 changes: 6 additions & 1 deletion src/ssgs/hugo.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,13 @@ export default class Hugo extends Ssg {
async generateBuildCommands(filePaths, options) {
const commands = await super.generateBuildCommands(filePaths, options);

commands.install.unshift({
value: 'export NODE_PATH=`pwd`/node_modules:$NODE_PATH',
attribution: 'workaround for a Hugo issue', // https://github.com/gohugoio/hugo/issues/9800
});

commands.build.unshift({
value: 'hugo',
value: 'hugo -b /',
attribution: 'most common for Hugo sites',
});
commands.output.unshift({
Expand Down
7 changes: 7 additions & 0 deletions src/ssgs/jekyll.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,13 @@ export default class Jekyll extends Ssg {
value: 'bundle exec jekyll build',
attribution: 'because of your Gemfile',
});

if (options.source) {
commands.environment['BUNDLE_GEMFILE'] = {
value: joinPaths([options.source, 'Gemfile']),
attribution: 'because of your Gemfile',
};
}
} else {
commands.build.unshift({
value: 'jekyll build',
Expand Down
29 changes: 29 additions & 0 deletions src/ssgs/lume.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import Ssg from './ssg.js';

export default class Lume extends Ssg {
constructor() {
super('lume');
}

/**
* Generates a list of build suggestions.
*
* @param filePaths {string[]} List of input file paths.
* @param options {{ config?: Record<string, any>; source?: string; readFile?: (path: string) => Promise<string | undefined>; }}
* @returns {Promise<import('../types.js').BuildCommands>}
*/
async generateBuildCommands(filePaths, options) {
const commands = await super.generateBuildCommands(filePaths, options);

commands.build.push({
value: 'deno task lume',
attribution: 'default for Lume sites',
});
commands.output.unshift({
value: '_site',
attribution: 'most common for Lume sites',
});

return commands;
}
}
29 changes: 29 additions & 0 deletions src/ssgs/mkdocs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import Ssg from './ssg.js';

export default class MkDocs extends Ssg {
constructor() {
super('mkdocs');
}

/**
* Generates a list of build suggestions.
*
* @param filePaths {string[]} List of input file paths.
* @param options {{ config?: Record<string, any>; source?: string; readFile?: (path: string) => Promise<string | undefined>; }}
* @returns {Promise<import('../types').BuildCommands>}
*/
async generateBuildCommands(filePaths, options) {
const commands = await super.generateBuildCommands(filePaths, options);

commands.build.push({
value: 'npx mkdocs build',
attribution: 'default for MkDocs sites',
});
commands.output.unshift({
value: 'site',
attribution: 'most common for MkDocs sites',
});

return commands;
}
}
22 changes: 22 additions & 0 deletions src/ssgs/next-js.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,26 @@ export default class NextJs extends Ssg {
'public/', // static assets
]);
}

/**
* Generates a list of build suggestions.
*
* @param filePaths {string[]} List of input file paths.
* @param options {{ config?: Record<string, any>; source?: string; readFile?: (path: string) => Promise<string | undefined>; }}
* @returns {Promise<import('../types').BuildCommands>}
*/
async generateBuildCommands(filePaths, options) {
const commands = await super.generateBuildCommands(filePaths, options);

commands.build.unshift({
value: 'npx next build && npx next export',
attribution: 'most common for Next.js sites',
});
commands.output.unshift({
value: 'out',
attribution: 'default for Next.js sites',
});

return commands;
}
}
29 changes: 29 additions & 0 deletions src/ssgs/nuxt-js.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import Ssg from './ssg.js';

export default class NuxtJs extends Ssg {
constructor() {
super('nuxtjs');
}

/**
* Generates a list of build suggestions.
*
* @param filePaths {string[]} List of input file paths.
* @param options {{ config?: Record<string, any>; source?: string; readFile?: (path: string) => Promise<string | undefined>; }}
* @returns {Promise<import('../types').BuildCommands>}
*/
async generateBuildCommands(filePaths, options) {
const commands = await super.generateBuildCommands(filePaths, options);

commands.build.push({
value: 'npx nuxt generate',
attribution: 'default for Nuxt sites',
});
commands.output.unshift({
value: 'dist',
attribution: 'most common for Nuxt sites',
});

return commands;
}
}
90 changes: 79 additions & 11 deletions src/ssgs/ssg.js
Original file line number Diff line number Diff line change
Expand Up @@ -466,27 +466,95 @@ export default class Ssg {

const packageJsonPath = joinPaths([options.source, 'package.json']);
if (filePaths.includes(packageJsonPath)) {
commands.install.push({
value: 'npm i',
attribution: 'because of your `package.json` file',
});
const useYarn =
filePaths.includes(joinPaths([options.source, 'yarn.lock'])) &&
!filePaths.includes(joinPaths([options.source, 'package-lock.json']));
if (useYarn) {
commands.install.push({
value: 'yarn',
attribution: 'because of your `yarn.lock` file',
});
} else {
commands.install.push({
value: 'npm i',
attribution: 'because of your `package.json` file',
});
}

commands.preserved.push({
value: 'node_modules/',
attribution: 'because of your `package.json` file',
});

try {
const raw = options.readFile ? await options.readFile(packageJsonPath) : undefined;
const parsed = raw ? JSON.parse(raw) : undefined;
if (options.readFile) {
const parsed = await parseDataFile(packageJsonPath, options.readFile);
if (parsed?.scripts?.build) {
commands.build.push({
value: 'npm run build',
attribution: 'found in your `package.json` file',
});
}
}
} catch (_e) {}
}

if (parsed?.scripts?.build) {
commands.build.push({
value: 'npm run build',
attribution: 'found in your `package.json` file',
/**
* Check a value from a settings file and add to build commands.
*
* @param value {unknown}
* @param filename {string}
* @param type {keyof import('../types').BuildCommands}
*/
const validateAndAddCommandFromSettings = (value, filename, type) => {
if (value && typeof value === 'string') {
if (type === 'environment') {
commands[type].value = {
value,
attribution: `found in your \`${filename}\` file`,
};
} else {
commands[type].push({
value,
attribution: `found in your \`${filename}\` file`,
});
}
} catch (_e) {}
}
};

if (options.readFile) {
const forestrySettingsPath = '.forestry/settings.yml';
if (filePaths.includes(forestrySettingsPath)) {
try {
const parsed = await parseDataFile(forestrySettingsPath, options.readFile);
validateAndAddCommandFromSettings(
parsed?.build?.install_dependencies_command,
forestrySettingsPath,
'install',
);
} catch (_e) {}
}

const netlifySettingsPath = 'netlify.toml';
if (filePaths.includes(netlifySettingsPath)) {
// https://docs.netlify.com/configure-builds/file-based-configuration/
try {
const parsed = await parseDataFile(netlifySettingsPath, options.readFile);
validateAndAddCommandFromSettings(parsed?.build?.command, netlifySettingsPath, 'build');
validateAndAddCommandFromSettings(parsed?.build?.publish, netlifySettingsPath, 'output');
} catch (_e) {}
}

const vercelSettingsPath = 'vercel.json';
if (filePaths.includes(vercelSettingsPath)) {
// https://vercel.com/docs/projects/project-configuration
try {
const parsed = await parseDataFile(vercelSettingsPath, options.readFile);
validateAndAddCommandFromSettings(parsed?.installCommand, vercelSettingsPath, 'install');
validateAndAddCommandFromSettings(parsed?.buildCommand, vercelSettingsPath, 'build');
validateAndAddCommandFromSettings(parsed?.outputDirectory, vercelSettingsPath, 'output');
} catch (_e) {}
}
}

return commands;
Expand Down
15 changes: 10 additions & 5 deletions src/ssgs/ssgs.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,27 @@ import NextJs from './next-js.js';
import Ssg from './ssg.js';
import Sveltekit from './sveltekit.js';
import Static from './static.js';
import Astro from './astro.js';
import NuxtJs from './nuxt-js.js';
import Gatsby from './gatsby.js';
import MkDocs from './mkdocs.js';
import Lume from './lume.js';

/** @type {Record<import('@cloudcannon/configuration-types').SsgKey, Ssg>} */
export const ssgs = {
hugo: new Hugo(),
jekyll: new Jekyll(),
eleventy: new Eleventy(),
nextjs: new NextJs(),
astro: new Ssg('astro'),
astro: new Astro(),
sveltekit: new Sveltekit(),
bridgetown: new Bridgetown(),
lume: new Ssg('lume'),
mkdocs: new Ssg('mkdocs'),
lume: new Lume(),
mkdocs: new MkDocs(),
docusaurus: new Ssg('docusaurus'),
gatsby: new Ssg('gatsby'),
gatsby: new Gatsby(),
hexo: new Ssg('hexo'),
nuxtjs: new Ssg('nuxtjs'),
nuxtjs: new NuxtJs(),
sphinx: new Ssg('sphinx'),
static: new Static(),
legacy: new Ssg('legacy'),
Expand Down
Loading

0 comments on commit 22dfaed

Please sign in to comment.