Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3eefe58

Browse files
Merge branch 'canary' into fix/basepath-public-folder-rewrite
2 parents 6f0b0be + 0226e78 commit 3eefe58

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

packages/create-next-app/create-app.ts

+12
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { install } from './helpers/install'
1919
import { isFolderEmpty } from './helpers/is-folder-empty'
2020
import { getOnline } from './helpers/is-online'
2121
import { shouldUseYarn } from './helpers/should-use-yarn'
22+
import { isWriteable } from './helpers/is-writeable'
2223

2324
export class DownloadError extends Error {}
2425

@@ -93,6 +94,17 @@ export async function createApp({
9394
}
9495

9596
const root = path.resolve(appPath)
97+
98+
if (!(await isWriteable(path.dirname(root)))) {
99+
console.error(
100+
'The application path is not writable, please check folder permissions and try again.'
101+
)
102+
console.error(
103+
'It is likely you do not have write permissions for this folder.'
104+
)
105+
process.exit(1)
106+
}
107+
96108
const appName = path.basename(root)
97109

98110
await makeDir(root)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import fs from 'fs'
2+
3+
export async function isWriteable(directory: string): Promise<boolean> {
4+
try {
5+
await fs.promises.access(directory, (fs.constants || fs).W_OK)
6+
return true
7+
} catch (err) {
8+
return false
9+
}
10+
}

test/integration/create-next-app/index.test.js

+26-2
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ const runStarter = (cwd, ...args) => {
2323
return res
2424
}
2525

26-
async function usingTempDir(fn) {
26+
async function usingTempDir(fn, options) {
2727
const folder = path.join(os.tmpdir(), Math.random().toString(36).substring(2))
28-
await fs.mkdirp(folder)
28+
await fs.mkdirp(folder, options)
2929
try {
3030
return await fn(folder)
3131
} finally {
@@ -251,4 +251,28 @@ describe('create next app', () => {
251251
}
252252
})
253253
})
254+
255+
it('should exit if the folder is not writable', async () => {
256+
await usingTempDir(async (cwd) => {
257+
const projectName = 'not-writable'
258+
expect.assertions(2)
259+
try {
260+
const res = await runStarter(cwd, projectName)
261+
262+
if (process.platform === 'win32') {
263+
expect(res.exitCode).toBe(0)
264+
expect(
265+
fs.existsSync(path.join(cwd, projectName, 'package.json'))
266+
).toBeTruthy()
267+
}
268+
} catch (e) {
269+
// eslint-disable-next-line jest/no-try-expect
270+
expect(e.exitCode).toBe(1)
271+
// eslint-disable-next-line jest/no-try-expect
272+
expect(e.stderr).toMatch(
273+
/you do not have write permissions for this folder/
274+
)
275+
}
276+
}, 0o500)
277+
})
254278
})

0 commit comments

Comments
 (0)