-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): auto-generate / update index.ts for exports
fix #1127 Signed-off-by: Taranveer Virk <[email protected]>
- Loading branch information
Showing
6 changed files
with
139 additions
and
23 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
1 change: 1 addition & 0 deletions
1
packages/cli/generators/app/templates/src/controllers/index.ts.ejs
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 @@ | ||
export * from './ping.controller'; |
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,23 @@ | ||
// Copyright IBM Corp. 2017,2018. All Rights Reserved. | ||
// Node module: @loopback/cli | ||
// This file is licensed under the MIT License. | ||
// License text available at https://opensource.org/licenses/MIT | ||
|
||
const path = require('path'); | ||
const util = require('util'); | ||
const fs = require('fs'); | ||
const appendFileAsync = util.promisify(fs.appendFile); | ||
|
||
/** | ||
* | ||
* @param {String} dir The directory in which index.ts is to be updated/created | ||
* @param {*} file The new file to be exported from index.ts | ||
*/ | ||
module.exports = async function(dir, file) { | ||
const indexFile = path.join(dir, 'index.ts'); | ||
if (!file.endsWith('.ts')) { | ||
throw new Error(`${file} must be a TypeScript (.ts) file`); | ||
} | ||
const content = `export * from './${file.slice(0, -3)}';\n`; | ||
await appendFileAsync(indexFile, 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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright IBM Corp. 2017,2018. All Rights Reserved. | ||
// Node module: @loopback/cli | ||
// This file is licensed under the MIT License. | ||
// License text available at https://opensource.org/licenses/MIT | ||
|
||
const updateIndex = require('../../lib/update-index'); | ||
const assert = require('yeoman-assert'); | ||
const path = require('path'); | ||
const util = require('util'); | ||
const fs = require('fs'); | ||
const writeFileAsync = util.promisify(fs.writeFile); | ||
|
||
const testlab = require('@loopback/testlab'); | ||
const expect = testlab.expect; | ||
const TestSandbox = testlab.TestSandbox; | ||
|
||
// Test Sandbox | ||
const SANDBOX_PATH = path.resolve(__dirname, '.sandbox'); | ||
const sandbox = new TestSandbox(SANDBOX_PATH); | ||
const expectedFile = path.join(SANDBOX_PATH, 'index.ts'); | ||
|
||
describe('update-index unit tests', () => { | ||
beforeEach('reset sandbox', () => sandbox.reset()); | ||
|
||
it('creates index.ts when not present', async () => { | ||
await updateIndex(SANDBOX_PATH, 'test.ts'); | ||
assert.file(expectedFile); | ||
assert.fileContent(expectedFile, /export \* from '.\/test';/); | ||
}); | ||
|
||
it('appends to existing index.ts when present', async () => { | ||
await writeFileAsync( | ||
path.join(SANDBOX_PATH, 'index.ts'), | ||
`export * from './first';\n`, | ||
); | ||
await updateIndex(SANDBOX_PATH, 'test.ts'); | ||
assert.file(expectedFile); | ||
assert.fileContent(expectedFile, /export \* from '.\/first'/); | ||
assert.fileContent(expectedFile, /export \* from '.\/test'/); | ||
}); | ||
|
||
it('throws an error when given a non-ts file', async () => { | ||
expect(updateIndex(SANDBOX_PATH, 'test.js')).to.be.rejectedWith( | ||
/test.js must be a TypeScript \(.ts\) file/, | ||
); | ||
}); | ||
}); |