Skip to content

add prettier & prettier-ignore & pre-commit hook #16

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

Merged
merged 1 commit into from
Oct 30, 2020
Merged
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
17 changes: 17 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Markdown
*.md

# Logs
logs
*.log
npm-debug.log*

# Dependency directories
node_modules

# Optional npm cache directory
.npm

# Optional REPL history
.node_repl_history
.next
1 change: 1 addition & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"@netlify/eslint-config-node/.prettierrc.json"
14 changes: 7 additions & 7 deletions helpers/isStaticExportProject.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
// the Next.js app uses static HTML export

const isStaticExportProject = ({ build, scripts }) => {
const NEXT_EXPORT_COMMAND = 'next export';
const isSetInNetlifyConfig = build && build.command && build.command.includes(NEXT_EXPORT_COMMAND);
const NEXT_EXPORT_COMMAND = 'next export'
const isSetInNetlifyConfig = build && build.command && build.command.includes(NEXT_EXPORT_COMMAND)
const isSetInNpmScript = Object.keys(scripts).find((script) => {
return scripts[script].includes(NEXT_EXPORT_COMMAND);
});
return isSetInNetlifyConfig || isSetInNpmScript;
};
return scripts[script].includes(NEXT_EXPORT_COMMAND)
})
return isSetInNetlifyConfig || isSetInNpmScript
}

module.exports = isStaticExportProject;
module.exports = isStaticExportProject
128 changes: 65 additions & 63 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const fs = require('fs');
const { existsSync, readFileSync } = require('fs');
const path = require('path');
const { appendFile, readdir } = require('fs').promises;
const { hasFramework } = require('@netlify/framework-info');
const nextOnNetlify = require('next-on-netlify');
const { PHASE_PRODUCTION_BUILD } = require('next/constants');
const { default: loadConfig } = require('next/dist/next-server/server/config');
const makeDir = require('make-dir');
const cpx = require('cpx');
const isStaticExportProject = require('./helpers/isStaticExportProject');
const fs = require('fs')
const { existsSync, readFileSync } = require('fs')
const path = require('path')
const { appendFile, readdir } = require('fs').promises
const { hasFramework } = require('@netlify/framework-info')
const nextOnNetlify = require('next-on-netlify')
const { PHASE_PRODUCTION_BUILD } = require('next/constants')
const { default: loadConfig } = require('next/dist/next-server/server/config')
const makeDir = require('make-dir')
const cpx = require('cpx')
const isStaticExportProject = require('./helpers/isStaticExportProject')

// * Helpful Plugin Context *
// - Between the prebuild and build steps, the project's build command is run
Expand All @@ -17,71 +17,73 @@ const isStaticExportProject = require('./helpers/isStaticExportProject');
module.exports = {
async onPreBuild({ netlifyConfig, packageJson: { scripts = {}, dependencies = {} }, utils }) {
if (!(await hasFramework('next'))) {
return failBuild(`This application does not use Next.js.`);
return failBuild(`This application does not use Next.js.`)
}

const { build } = netlifyConfig;
const { failBuild } = utils.build;
const { build } = netlifyConfig
const { failBuild } = utils.build

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

if (isStaticExportProject({ build, scripts })) {
failBuild(`** Static HTML export next.js projects do not require this plugin **`);
}
if (isStaticExportProject({ build, scripts })) {
failBuild(`** Static HTML export next.js projects do not require this plugin **`)
}

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

const isFunctionsDirectoryCorrect = build && build.functions && build.functions === path.resolve('out_functions');
if (!isFunctionsDirectoryCorrect) {
// to do rephrase
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`);
}
const isFunctionsDirectoryCorrect = build && build.functions && build.functions === path.resolve('out_functions')
if (!isFunctionsDirectoryCorrect) {
// to do rephrase
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`,
)
}

if (existsSync('next.config.js')) {
// If the next config exists, fail build if target isnt in acceptableTargets
const acceptableTargets = ['serverless', 'experimental-serverless-trace'];
const nextConfig = loadConfig(PHASE_PRODUCTION_BUILD, path.resolve('.'));
const isValidTarget = acceptableTargets.includes(nextConfig.target);
if (!isValidTarget) {
failBuild(`next.config.js must be one of: ${acceptableTargets.join(', ')}`);
}
} else {
// Create the next config file with target set to serverless by default
const nextConfig = `
if (existsSync('next.config.js')) {
// If the next config exists, fail build if target isnt in acceptableTargets
const acceptableTargets = ['serverless', 'experimental-serverless-trace']
const nextConfig = loadConfig(PHASE_PRODUCTION_BUILD, path.resolve('.'))
const isValidTarget = acceptableTargets.includes(nextConfig.target)
if (!isValidTarget) {
failBuild(`next.config.js must be one of: ${acceptableTargets.join(', ')}`)
}
} else {
// Create the next config file with target set to serverless by default
const nextConfig = `
module.exports = {
target: 'serverless'
}
`;
await appendFile('next.config.js', nextConfig);
console.log(`** Adding next.config.js with target set to 'serverless' **`);
}
`
await appendFile('next.config.js', nextConfig)
console.log(`** Adding next.config.js with target set to 'serverless' **`)
}
},
async onBuild({ constants }) {
console.log(`** Running Next on Netlify package **`);
nextOnNetlify();
console.log(`** Running Next on Netlify package **`)
nextOnNetlify()

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

// TO-DO: use FUNCTIONS_DIST when internal bug is fixed
const { PUBLISH_DIR } = constants;
// if (!existsSync(FUNCTIONS_DIST)) {
// await makeDir(FUNCTIONS_DIST);
// }
if (!existsSync(PUBLISH_DIR)) {
await makeDir(PUBLISH_DIR);
}
// TO-DO: use FUNCTIONS_DIST when internal bug is fixed
const { PUBLISH_DIR } = constants
// if (!existsSync(FUNCTIONS_DIST)) {
// await makeDir(FUNCTIONS_DIST);
// }
if (!existsSync(PUBLISH_DIR)) {
await makeDir(PUBLISH_DIR)
}

// TO-DO: make sure FUNCTIONS_DIST doesnt have a custom function name conflict
// with function names that next-on-netlify can generate
// cpx.copySync('out_functions/**/*', FUNCTIONS_SRC);
cpx.copySync('out_publish/**/*', PUBLISH_DIR);
}
// TO-DO: make sure FUNCTIONS_DIST doesnt have a custom function name conflict
// with function names that next-on-netlify can generate
// cpx.copySync('out_functions/**/*', FUNCTIONS_SRC);
cpx.copySync('out_publish/**/*', PUBLISH_DIR)
},
}
Loading