Unverified Commit 0226e787 authored by matamatanot's avatar matamatanot Committed by GitHub

CNA: Add warning about permission (#14889)

close #14744

<img width="851" alt="screen_shot" src="https://user-images.githubusercontent.com/39780486/86603491-06bec800-bfdf-11ea-9928-ee85cbad86a7.png">


I have some concerns.

- `import { isWriteable } from '../next/build/is-writeable'` **not** from `create-next-app ` package.
- The warning sentence is from npm. Not for Next.js. I'm not a native English speaker. I'd like to know the natural expression for this.
parent 1ad9cd90
...@@ -19,6 +19,7 @@ import { install } from './helpers/install' ...@@ -19,6 +19,7 @@ import { install } from './helpers/install'
import { isFolderEmpty } from './helpers/is-folder-empty' import { isFolderEmpty } from './helpers/is-folder-empty'
import { getOnline } from './helpers/is-online' import { getOnline } from './helpers/is-online'
import { shouldUseYarn } from './helpers/should-use-yarn' import { shouldUseYarn } from './helpers/should-use-yarn'
import { isWriteable } from './helpers/is-writeable'
export class DownloadError extends Error {} export class DownloadError extends Error {}
...@@ -93,6 +94,17 @@ export async function createApp({ ...@@ -93,6 +94,17 @@ export async function createApp({
} }
const root = path.resolve(appPath) const root = path.resolve(appPath)
if (!(await isWriteable(path.dirname(root)))) {
console.error(
'The application path is not writable, please check folder permissions and try again.'
)
console.error(
'It is likely you do not have write permissions for this folder.'
)
process.exit(1)
}
const appName = path.basename(root) const appName = path.basename(root)
await makeDir(root) await makeDir(root)
......
import fs from 'fs'
export async function isWriteable(directory: string): Promise<boolean> {
try {
await fs.promises.access(directory, (fs.constants || fs).W_OK)
return true
} catch (err) {
return false
}
}
...@@ -23,9 +23,9 @@ const runStarter = (cwd, ...args) => { ...@@ -23,9 +23,9 @@ const runStarter = (cwd, ...args) => {
return res return res
} }
async function usingTempDir(fn) { async function usingTempDir(fn, options) {
const folder = path.join(os.tmpdir(), Math.random().toString(36).substring(2)) const folder = path.join(os.tmpdir(), Math.random().toString(36).substring(2))
await fs.mkdirp(folder) await fs.mkdirp(folder, options)
try { try {
return await fn(folder) return await fn(folder)
} finally { } finally {
...@@ -251,4 +251,28 @@ describe('create next app', () => { ...@@ -251,4 +251,28 @@ describe('create next app', () => {
} }
}) })
}) })
it('should exit if the folder is not writable', async () => {
await usingTempDir(async (cwd) => {
const projectName = 'not-writable'
expect.assertions(2)
try {
const res = await runStarter(cwd, projectName)
if (process.platform === 'win32') {
expect(res.exitCode).toBe(0)
expect(
fs.existsSync(path.join(cwd, projectName, 'package.json'))
).toBeTruthy()
}
} catch (e) {
// eslint-disable-next-line jest/no-try-expect
expect(e.exitCode).toBe(1)
// eslint-disable-next-line jest/no-try-expect
expect(e.stderr).toMatch(
/you do not have write permissions for this folder/
)
}
}, 0o500)
})
}) })
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment