forked from strapi/strapi
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b5c0aef
commit e4ab3d3
Showing
7 changed files
with
117 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module.exports = { | ||
ensureFile: jest.fn(() => Promise.resolve()), | ||
writeFile: jest.fn(() => Promise.resolve()), | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
const fs = require('../fs'); | ||
jest.mock('fs-extra'); | ||
const fsExtra = require('fs-extra'); | ||
|
||
describe('Strapi fs utils', () => { | ||
const strapi = { | ||
dir: '/tmp', | ||
}; | ||
|
||
test('Provides new functions', () => { | ||
const strapiFS = fs(strapi); | ||
|
||
expect(strapiFS.writeAppFile).toBeInstanceOf(Function); | ||
expect(strapiFS.writePluginFile).toBeInstanceOf(Function); | ||
}); | ||
|
||
describe('Write App File', () => { | ||
test('Makes sure the path exists and writes', async () => { | ||
const strapiFS = fs(strapi); | ||
|
||
const content = ''; | ||
|
||
await strapiFS.writeAppFile('test', content); | ||
|
||
expect(fsExtra.ensureFile).toHaveBeenCalledWith('/tmp/test'); | ||
expect(fsExtra.writeFile).toHaveBeenCalledWith('/tmp/test', content); | ||
}); | ||
|
||
test('Normalize the path to avoid relative access to folders in parent directories', async () => { | ||
const strapiFS = fs(strapi); | ||
|
||
const content = ''; | ||
|
||
await strapiFS.writeAppFile('../../test', content); | ||
|
||
expect(fsExtra.ensureFile).toHaveBeenCalledWith('/tmp/test'); | ||
expect(fsExtra.writeFile).toHaveBeenCalledWith('/tmp/test', content); | ||
}); | ||
|
||
test('Works with array path', async () => { | ||
const strapiFS = fs(strapi); | ||
|
||
const content = ''; | ||
|
||
await strapiFS.writeAppFile(['test', 'sub', 'path'], content); | ||
|
||
expect(fsExtra.ensureFile).toHaveBeenCalledWith('/tmp/test/sub/path'); | ||
expect(fsExtra.writeFile).toHaveBeenCalledWith( | ||
'/tmp/test/sub/path', | ||
content | ||
); | ||
}); | ||
}); | ||
|
||
describe('Write Plugin File', () => { | ||
test('Scopes the writes in the extensions folder', async () => { | ||
const strapiFS = fs(strapi); | ||
|
||
const content = ''; | ||
|
||
strapiFS.writeAppFile = jest.fn(() => Promise.resolve()); | ||
|
||
await strapiFS.writePluginFile( | ||
'users-permissions', | ||
['test', 'sub', 'path'], | ||
content | ||
); | ||
|
||
expect(strapiFS.writeAppFile).toHaveBeenCalledWith( | ||
'extensions/users-permissions/test/sub/path', | ||
content | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,41 @@ | ||
'use strict'; | ||
|
||
const path = require('path'); | ||
const fs = require('fs-extra'); | ||
|
||
/** | ||
* create strapi fs layer | ||
*/ | ||
module.exports = strapi => { | ||
/** | ||
* Writes a file in a strapi app | ||
* @param {Array|string} optPath - file path | ||
* @param {string} data - content | ||
*/ | ||
const writeAppFile = (optPath, data) => { | ||
const filePath = Array.isArray(optPath) ? optPath.join('/') : optPath; | ||
const strapiFS = { | ||
/** | ||
* Writes a file in a strapi app | ||
* @param {Array|string} optPath - file path | ||
* @param {string} data - content | ||
*/ | ||
writeAppFile(optPath, data) { | ||
const filePath = Array.isArray(optPath) ? optPath.join('/') : optPath; | ||
|
||
const normalizedPath = path.normalize(filePath).replace(/^(\/?\.\.?)+/, ''); | ||
const normalizedPath = path | ||
.normalize(filePath) | ||
.replace(/^(\/?\.\.?)+/, ''); | ||
|
||
const writePath = path.join(strapi.config.appPath, normalizedPath); | ||
const writePath = path.join(strapi.dir, normalizedPath); | ||
|
||
return fs.ensureFile(writePath).then(() => fs.writeFile(writePath, data)); | ||
}; | ||
return fs.ensureFile(writePath).then(() => fs.writeFile(writePath, data)); | ||
}, | ||
|
||
/** | ||
* Writes a file in a plugin extensions folder | ||
* @param {string} plugin - plugin name | ||
* @param {Array|string} optPath - path to file | ||
* @param {string} data - content | ||
*/ | ||
const writePluginFile = (plugin, optPath, data) => { | ||
const newPath = ['extensions', plugin].concat(optPath).join('/'); | ||
return writeAppFile(newPath, data); | ||
/** | ||
* Writes a file in a plugin extensions folder | ||
* @param {string} plugin - plugin name | ||
* @param {Array|string} optPath - path to file | ||
* @param {string} data - content | ||
*/ | ||
writePluginFile(plugin, optPath, data) { | ||
const newPath = ['extensions', plugin].concat(optPath).join('/'); | ||
return strapiFS.writeAppFile(newPath, data); | ||
}, | ||
}; | ||
|
||
return Object.assign(fs, { | ||
writeAppFile, | ||
writePluginFile, | ||
}); | ||
return strapiFS; | ||
}; |