Skip to content

Commit

Permalink
CNA: Add warning about permission (#14889)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
matamatanot committed Aug 19, 2020
1 parent 1ad9cd9 commit 0226e78
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
12 changes: 12 additions & 0 deletions packages/create-next-app/create-app.ts
Expand Up @@ -19,6 +19,7 @@ import { install } from './helpers/install'
import { isFolderEmpty } from './helpers/is-folder-empty'
import { getOnline } from './helpers/is-online'
import { shouldUseYarn } from './helpers/should-use-yarn'
import { isWriteable } from './helpers/is-writeable'

export class DownloadError extends Error {}

Expand Down Expand Up @@ -93,6 +94,17 @@ export async function createApp({
}

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)

await makeDir(root)
Expand Down
10 changes: 10 additions & 0 deletions packages/create-next-app/helpers/is-writeable.ts
@@ -0,0 +1,10 @@
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
}
}
28 changes: 26 additions & 2 deletions test/integration/create-next-app/index.test.js
Expand Up @@ -23,9 +23,9 @@ const runStarter = (cwd, ...args) => {
return res
}

async function usingTempDir(fn) {
async function usingTempDir(fn, options) {
const folder = path.join(os.tmpdir(), Math.random().toString(36).substring(2))
await fs.mkdirp(folder)
await fs.mkdirp(folder, options)
try {
return await fn(folder)
} finally {
Expand Down Expand Up @@ -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)
})
})

0 comments on commit 0226e78

Please sign in to comment.