-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(cli): auto-generate index.ts for exports #1363
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './ping.controller'; | ||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not use this instead? https://nodejs.org/api/fs.html#fs_fs_appendfilesync_path_data_options There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
In a generator you can call Here's a Node.js article on blocking / non-blocking. https://nodejs.org/en/docs/guides/blocking-vs-non-blocking/ |
||
|
||
/** | ||
* | ||
* @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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we explicitly check whether the line exists already? Or is it that too much of an edge case There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe. My assumption is to let the generator handle that -- within a generator this function is triggered only when the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nvm, I forgot about yeoman's own conflicter system |
||
}; |
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()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible to just provide There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've tried and I get the following error message if I do
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like it has to do with scoping :(. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like the current format -- it's how we use |
||
|
||
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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. needs to be wrapped in an anonymous function There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I was thinking of error assertions. pls ignore |
||
/test.js must be a TypeScript \(.ts\) file/, | ||
); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This makes me wonder whether it should be included when the app is generated as opposed to adding it on when the controller generator is run. Thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check the file -- It's in the
app
generator and this is just the defaultindex.ts
that gets generated since we are generatingping.controller
in new apps.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The ping controller I know about, but I was wondering about whether we should include this
index.ts
file when the app generator is runThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea when the app generator is run there will be a
src/controllers/index.ts
that will be generated which exports the ping controller.