Skip to content

Commit 3cff662

Browse files
committed
add prettier & prettier-ignore & pre-commit hook
1 parent 8efbebb commit 3cff662

File tree

5 files changed

+331
-76
lines changed

5 files changed

+331
-76
lines changed

.prettierignore

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Markdown
2+
*.md
3+
4+
# Logs
5+
logs
6+
*.log
7+
npm-debug.log*
8+
9+
# Dependency directories
10+
node_modules
11+
12+
# Optional npm cache directory
13+
.npm
14+
15+
# Optional REPL history
16+
.node_repl_history
17+
.next

helpers/isStaticExportProject.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
// the Next.js app uses static HTML export
44

55
const isStaticExportProject = ({ build, scripts }) => {
6-
const NEXT_EXPORT_COMMAND = 'next export';
7-
const isSetInNetlifyConfig = build && build.command && build.command.includes(NEXT_EXPORT_COMMAND);
6+
const NEXT_EXPORT_COMMAND = "next export";
7+
const isSetInNetlifyConfig =
8+
build && build.command && build.command.includes(NEXT_EXPORT_COMMAND);
89
const isSetInNpmScript = Object.keys(scripts).find((script) => {
910
return scripts[script].includes(NEXT_EXPORT_COMMAND);
1011
});

index.js

Lines changed: 93 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,121 @@
1-
const fs = require('fs');
2-
const { existsSync, readFileSync } = require('fs');
3-
const path = require('path');
4-
const { appendFile, readdir } = require('fs').promises;
5-
const { hasFramework } = require('@netlify/framework-info');
6-
const nextOnNetlify = require('next-on-netlify');
7-
const { PHASE_PRODUCTION_BUILD } = require('next/constants');
8-
const { default: loadConfig } = require('next/dist/next-server/server/config');
9-
const makeDir = require('make-dir');
10-
const cpx = require('cpx');
11-
const isStaticExportProject = require('./helpers/isStaticExportProject');
1+
const fs = require("fs");
2+
const { existsSync, readFileSync } = require("fs");
3+
const path = require("path");
4+
const { appendFile, readdir } = require("fs").promises;
5+
const { hasFramework } = require("@netlify/framework-info");
6+
const nextOnNetlify = require("next-on-netlify");
7+
const { PHASE_PRODUCTION_BUILD } = require("next/constants");
8+
const { default: loadConfig } = require("next/dist/next-server/server/config");
9+
const makeDir = require("make-dir");
10+
const cpx = require("cpx");
11+
const isStaticExportProject = require("./helpers/isStaticExportProject");
1212

1313
// * Helpful Plugin Context *
1414
// - Between the prebuild and build steps, the project's build command is run
1515
// - Between the build and postbuild steps, any functions are bundled
1616

1717
module.exports = {
1818
async onPreBuild({ constants, netlifyConfig, utils }) {
19-
if (!(await hasFramework('next'))) {
19+
if (!(await hasFramework("next"))) {
2020
return failBuild(`This application does not use Next.js.`);
2121
}
2222

2323
const { build } = netlifyConfig;
2424
const { failBuild } = utils.build;
2525

26-
// TO-DO: Post alpha, try to remove this workaround for missing deps in
27-
// the next-on-netlify function template
28-
await utils.run.command('npm install next-on-netlify@latest');
26+
// TO-DO: Post alpha, try to remove this workaround for missing deps in
27+
// the next-on-netlify function template
28+
await utils.run.command("npm install next-on-netlify@latest");
2929

30-
// Require the project's package.json for access to its scripts
31-
// and dependencies in order to check existing project configuration
32-
let packageJson;
33-
if (existsSync(path.resolve(constants.PUBLISH_DIR, 'package.json'))) {
34-
packageJson = require(path.resolve(constants.PUBLISH_DIR, 'package.json'));
35-
} else if (existsSync(path.resolve(constants.PUBLISH_DIR, '..', 'package.json'))) {
36-
packageJson = require(path.resolve(constants.PUBLISH_DIR, '..', 'package.json'));
37-
} else {
38-
failBuild(`Cannot locate your package.json file. Please make sure your package.json is
39-
at the root of your project or in your publish directory.`
40-
);
41-
}
30+
// Require the project's package.json for access to its scripts
31+
// and dependencies in order to check existing project configuration
32+
let packageJson;
33+
if (existsSync(path.resolve(constants.PUBLISH_DIR, "package.json"))) {
34+
packageJson = require(path.resolve(
35+
constants.PUBLISH_DIR,
36+
"package.json"
37+
));
38+
} else if (
39+
existsSync(path.resolve(constants.PUBLISH_DIR, "..", "package.json"))
40+
) {
41+
packageJson = require(path.resolve(
42+
constants.PUBLISH_DIR,
43+
"..",
44+
"package.json"
45+
));
46+
} else {
47+
failBuild(`Cannot locate your package.json file. Please make sure your package.json is
48+
at the root of your project or in your publish directory.`);
49+
}
4250

43-
const { scripts, dependencies } = packageJson;
51+
const { scripts, dependencies } = packageJson;
4452

45-
if (isStaticExportProject({ build, scripts })) {
46-
failBuild(`** Static HTML export next.js projects do not require this plugin **`);
47-
}
53+
if (isStaticExportProject({ build, scripts })) {
54+
failBuild(
55+
`** Static HTML export next.js projects do not require this plugin **`
56+
);
57+
}
4858

49-
// TO-DO: check scripts to make sure the app isn't manually running NoN
50-
// For now, we'll make it clear in the README
51-
// const isAlreadyUsingNextOnNetlify = Object.keys(dependencies).find((dep) => dep === 'next-on-netlify');
52-
// if (isAlreadyUsingNextOnNetlify) {
53-
// failBuild(`This plugin cannot support apps that manually use next-on-netlify. Uninstall next-on-netlify as a dependency to resolve.`);
54-
// }
59+
// TO-DO: check scripts to make sure the app isn't manually running NoN
60+
// For now, we'll make it clear in the README
61+
// const isAlreadyUsingNextOnNetlify = Object.keys(dependencies).find((dep) => dep === 'next-on-netlify');
62+
// if (isAlreadyUsingNextOnNetlify) {
63+
// failBuild(`This plugin cannot support apps that manually use next-on-netlify. Uninstall next-on-netlify as a dependency to resolve.`);
64+
// }
5565

56-
const isFunctionsDirectoryCorrect = build && build.functions && build.functions === path.resolve('out_functions');
57-
if (!isFunctionsDirectoryCorrect) {
58-
// to do rephrase
59-
failBuild(`You must designate a functions directory named "out_functions" in your netlify.toml or in your app's build settings on Netlify. See docs for more info: https://docs.netlify.com/functions/configure-and-deploy/#configure-the-functions-folder`);
60-
}
66+
const isFunctionsDirectoryCorrect =
67+
build &&
68+
build.functions &&
69+
build.functions === path.resolve("out_functions");
70+
if (!isFunctionsDirectoryCorrect) {
71+
// to do rephrase
72+
failBuild(
73+
`You must designate a functions directory named "out_functions" in your netlify.toml or in your app's build settings on Netlify. See docs for more info: https://docs.netlify.com/functions/configure-and-deploy/#configure-the-functions-folder`
74+
);
75+
}
6176

62-
if (existsSync('next.config.js')) {
63-
// If the next config exists, fail build if target isnt in acceptableTargets
64-
const acceptableTargets = ['serverless', 'experimental-serverless-trace'];
65-
const nextConfig = loadConfig(PHASE_PRODUCTION_BUILD, path.resolve('.'));
66-
const isValidTarget = acceptableTargets.includes(nextConfig.target);
67-
if (!isValidTarget) {
68-
failBuild(`next.config.js must be one of: ${acceptableTargets.join(', ')}`);
69-
}
70-
} else {
71-
// Create the next config file with target set to serverless by default
72-
const nextConfig = `
77+
if (existsSync("next.config.js")) {
78+
// If the next config exists, fail build if target isnt in acceptableTargets
79+
const acceptableTargets = ["serverless", "experimental-serverless-trace"];
80+
const nextConfig = loadConfig(PHASE_PRODUCTION_BUILD, path.resolve("."));
81+
const isValidTarget = acceptableTargets.includes(nextConfig.target);
82+
if (!isValidTarget) {
83+
failBuild(
84+
`next.config.js must be one of: ${acceptableTargets.join(", ")}`
85+
);
86+
}
87+
} else {
88+
// Create the next config file with target set to serverless by default
89+
const nextConfig = `
7390
module.exports = {
7491
target: 'serverless'
7592
}
7693
`;
77-
await appendFile('next.config.js', nextConfig);
78-
console.log(`** Adding next.config.js with target set to 'serverless' **`);
79-
}
94+
await appendFile("next.config.js", nextConfig);
95+
console.log(
96+
`** Adding next.config.js with target set to 'serverless' **`
97+
);
98+
}
8099
},
81100
async onBuild({ constants }) {
82-
console.log(`** Running Next on Netlify package **`);
83-
nextOnNetlify();
101+
console.log(`** Running Next on Netlify package **`);
102+
nextOnNetlify();
84103

85-
// Next-on-netlify puts its files into out_functions and out_publish
86-
// Copy files from next-on-netlify's output to the right functions/publish dirs
104+
// Next-on-netlify puts its files into out_functions and out_publish
105+
// Copy files from next-on-netlify's output to the right functions/publish dirs
87106

88-
// TO-DO: use FUNCTIONS_DIST when internal bug is fixed
89-
const { PUBLISH_DIR } = constants;
90-
// if (!existsSync(FUNCTIONS_DIST)) {
91-
// await makeDir(FUNCTIONS_DIST);
92-
// }
93-
if (!existsSync(PUBLISH_DIR)) {
94-
await makeDir(PUBLISH_DIR);
95-
}
107+
// TO-DO: use FUNCTIONS_DIST when internal bug is fixed
108+
const { PUBLISH_DIR } = constants;
109+
// if (!existsSync(FUNCTIONS_DIST)) {
110+
// await makeDir(FUNCTIONS_DIST);
111+
// }
112+
if (!existsSync(PUBLISH_DIR)) {
113+
await makeDir(PUBLISH_DIR);
114+
}
96115

97-
// TO-DO: make sure FUNCTIONS_DIST doesnt have a custom function name conflict
98-
// with function names that next-on-netlify can generate
99-
// cpx.copySync('out_functions/**/*', FUNCTIONS_SRC);
100-
cpx.copySync('out_publish/**/*', PUBLISH_DIR);
101-
}
102-
}
116+
// TO-DO: make sure FUNCTIONS_DIST doesnt have a custom function name conflict
117+
// with function names that next-on-netlify can generate
118+
// cpx.copySync('out_functions/**/*', FUNCTIONS_SRC);
119+
cpx.copySync("out_publish/**/*", PUBLISH_DIR);
120+
},
121+
};

0 commit comments

Comments
 (0)