-
Notifications
You must be signed in to change notification settings - Fork 994
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
Add destroy command #487
Merged
Merged
Add destroy command #487
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
f9ce118
WIP: add destroy for components (#80)
antonmoiseev f25c9a2
WIP: fix import path
antonmoiseev 43d66cc
WIP: move destroy component command's file into subfolder.
antonmoiseev 79a93b5
WIP: add `destroy service` command
antonmoiseev 385ffe3
WIP: add destroy for cell and layout components
antonmoiseev b1956c2
WIP: improve desc for generate and destroy tasks
antonmoiseev b0a2f7b
WIP: add destroy for page components
antonmoiseev f5a1316
WIP: add destroy for sdl components
antonmoiseev e8a426e
WIP: add destroy for scaffold components
antonmoiseev bd9b5b6
WIP: clean up empty dirs after destroy
antonmoiseev ffe5fc2
Minor cleanup, renaming
antonmoiseev 4bf03fb
Add test for destroy page command
antonmoiseev dd1a6b4
Split page destroy test into three
antonmoiseev 13581a0
Merge branch 'master' into issue80
antonmoiseev f4ac09c
Adjust scaffold destroy to #423 changes
antonmoiseev ae7037c
Add test for destroy component command
antonmoiseev 4b54fc3
Add test for destroy cell command
antonmoiseev a412fe4
Add test for destroy layout command
antonmoiseev db31736
Add test for destroy service command
antonmoiseev 92f2c31
Add test for destroy scaffold command
antonmoiseev 864243e
Merge branch 'master' into issue80
antonmoiseev e4bd971
Add test for destroy sdl command
antonmoiseev 2ca9be8
Add destroy function command
antonmoiseev 49b8568
Add test for destroy function command
antonmoiseev d99e469
Merge branch 'master' into issue80
antonmoiseev b3475f2
Fix destroy scaffold after removing --path flag
antonmoiseev 6f7f2d2
Merge branch 'master' into issue80
antonmoiseev f5685fa
Merge branch 'master' into issue80
antonmoiseev b183ff7
Merge branch 'master' into issue80
antonmoiseev 93d3b82
Merge branch 'master' into issue80
peterp 18f29d7
Remove `argv` to make command work.
peterp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { isString } from 'lodash' | ||
|
||
const fs = { | ||
...require.requireActual('fs'), | ||
} | ||
|
||
// This is a custom function that our tests can use during setup to specify | ||
// what the files on the "mock" filesystem should look like when any of the | ||
// `fs` APIs are used. | ||
let mockFiles = {} | ||
/** @param newMockFiles - {[filepath]: contents} */ | ||
fs.__setMockFiles = (newMockFiles) => { | ||
mockFiles = { ...newMockFiles } | ||
} | ||
|
||
fs.existsSync = (path) => { | ||
return isString(mockFiles[path]) | ||
} | ||
|
||
fs.mkdirSync = () => {} | ||
|
||
fs.readFileSync = (path) => { | ||
return mockFiles[path] | ||
} | ||
|
||
fs.writeFileSync = (path, contents) => { | ||
mockFiles[path] = contents | ||
} | ||
|
||
fs.unlinkSync = (path) => { | ||
delete mockFiles[path] | ||
} | ||
|
||
module.exports = fs |
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,6 @@ | ||
export const command = 'destroy <type>' | ||
export const aliases = ['d'] | ||
export const desc = 'Rollback changes made by the generate command.' | ||
|
||
export const builder = (yargs) => | ||
yargs.commandDir('./destroy', { recurse: true }).demandCommand() |
36 changes: 36 additions & 0 deletions
36
packages/cli/src/commands/destroy/cell/__tests__/cell.test.js
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,36 @@ | ||
global.__dirname = __dirname | ||
jest.mock('fs') | ||
jest.mock('src/lib', () => { | ||
return { | ||
...require.requireActual('src/lib'), | ||
generateTemplate: () => '', | ||
} | ||
}) | ||
|
||
import fs from 'fs' | ||
|
||
import 'src/lib/test' | ||
|
||
import { files } from '../../../generate/cell/cell' | ||
import { tasks } from '../cell' | ||
|
||
beforeEach(() => { | ||
fs.__setMockFiles(files({ name: 'User' })) | ||
}) | ||
|
||
afterEach(() => { | ||
fs.__setMockFiles({}) | ||
jest.spyOn(fs, 'unlinkSync').mockClear() | ||
}) | ||
|
||
test('destroys cell files', async () => { | ||
const unlinkSpy = jest.spyOn(fs, 'unlinkSync') | ||
const t = tasks({ componentName: 'cell', filesFn: files, name: 'User' }) | ||
t.setRenderer('silent') | ||
|
||
return t.run().then(() => { | ||
const generatedFiles = Object.keys(files({ name: 'User' })) | ||
expect(generatedFiles.length).toEqual(unlinkSpy.mock.calls.length) | ||
generatedFiles.forEach((f) => expect(unlinkSpy).toHaveBeenCalledWith(f)) | ||
}) | ||
}) |
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,13 @@ | ||
import { files as cellFiles } from '../../generate/cell/cell' | ||
import { createYargsForComponentDestroy } from '../helpers' | ||
|
||
export const { | ||
command, | ||
desc, | ||
builder, | ||
handler, | ||
tasks, | ||
} = createYargsForComponentDestroy({ | ||
componentName: 'cell', | ||
filesFn: cellFiles, | ||
}) |
36 changes: 36 additions & 0 deletions
36
packages/cli/src/commands/destroy/component/__tests__/component.test.js
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,36 @@ | ||
global.__dirname = __dirname | ||
jest.mock('fs') | ||
jest.mock('src/lib', () => { | ||
return { | ||
...require.requireActual('src/lib'), | ||
generateTemplate: () => '', | ||
} | ||
}) | ||
|
||
import fs from 'fs' | ||
|
||
import 'src/lib/test' | ||
|
||
import { files } from '../../../generate/component/component' | ||
import { tasks } from '../component' | ||
|
||
beforeEach(() => { | ||
fs.__setMockFiles(files({ name: 'About' })) | ||
}) | ||
|
||
afterEach(() => { | ||
fs.__setMockFiles({}) | ||
jest.spyOn(fs, 'unlinkSync').mockClear() | ||
}) | ||
|
||
test('destroys component files', async () => { | ||
const unlinkSpy = jest.spyOn(fs, 'unlinkSync') | ||
const t = tasks({ componentName: 'component', filesFn: files, name: 'About' }) | ||
t.setRenderer('silent') | ||
|
||
return t.run().then(() => { | ||
const generatedFiles = Object.keys(files({ name: 'About' })) | ||
expect(generatedFiles.length).toEqual(unlinkSpy.mock.calls.length) | ||
generatedFiles.forEach((f) => expect(unlinkSpy).toHaveBeenCalledWith(f)) | ||
}) | ||
}) |
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,14 @@ | ||
import { files as componentFiles } from '../../generate/component/component' | ||
import { createYargsForComponentDestroy } from '../helpers' | ||
|
||
export const desc = 'Destroy a component.' | ||
|
||
export const { | ||
command, | ||
builder, | ||
handler, | ||
tasks, | ||
} = createYargsForComponentDestroy({ | ||
componentName: 'component', | ||
filesFn: componentFiles, | ||
}) |
40 changes: 40 additions & 0 deletions
40
packages/cli/src/commands/destroy/function/__tests__/function.test.js
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,40 @@ | ||
global.__dirname = __dirname | ||
jest.mock('fs') | ||
jest.mock('src/lib', () => { | ||
return { | ||
...require.requireActual('src/lib'), | ||
generateTemplate: () => '', | ||
} | ||
}) | ||
|
||
import fs from 'fs' | ||
|
||
import 'src/lib/test' | ||
|
||
import { files } from '../../../generate/function/function' | ||
import { tasks } from '../function' | ||
|
||
beforeEach(async () => { | ||
fs.__setMockFiles(await files({ name: 'sendMail' })) | ||
}) | ||
|
||
afterEach(() => { | ||
fs.__setMockFiles({}) | ||
jest.spyOn(fs, 'unlinkSync').mockClear() | ||
}) | ||
|
||
test('destroys service files', async () => { | ||
const unlinkSpy = jest.spyOn(fs, 'unlinkSync') | ||
const t = tasks({ | ||
componentName: 'service', | ||
filesFn: files, | ||
name: 'sendMail', | ||
}) | ||
t.setRenderer('silent') | ||
|
||
return t.run().then(async () => { | ||
const generatedFiles = Object.keys(await files({ name: 'sendMail' })) | ||
expect(generatedFiles.length).toEqual(unlinkSpy.mock.calls.length) | ||
generatedFiles.forEach((f) => expect(unlinkSpy).toHaveBeenCalledWith(f)) | ||
}) | ||
}) |
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,14 @@ | ||
import { files as functionFiles } from '../../generate/function/function' | ||
import { createYargsForComponentDestroy } from '../helpers' | ||
|
||
export const desc = 'Destroy a function' | ||
|
||
export const { | ||
builder, | ||
command, | ||
handler, | ||
tasks, | ||
} = createYargsForComponentDestroy({ | ||
componentName: 'function', | ||
filesFn: functionFiles, | ||
}) |
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,35 @@ | ||
import Listr from 'listr' | ||
|
||
import { deleteFilesTask } from 'src/lib' | ||
import c from 'src/lib/colors' | ||
|
||
const tasks = ({ componentName, filesFn, name }) => | ||
new Listr( | ||
[ | ||
{ | ||
title: `Destroying ${componentName} files...`, | ||
task: async () => { | ||
const f = await filesFn({ name }) | ||
return deleteFilesTask(f) | ||
}, | ||
}, | ||
], | ||
{ collapse: false, exitOnError: true } | ||
) | ||
|
||
export const createYargsForComponentDestroy = ({ componentName, filesFn }) => { | ||
return { | ||
command: `${componentName} <name>`, | ||
desc: `Destroy a ${componentName} component.`, | ||
handler: async ({ name }) => { | ||
const t = tasks({ componentName, filesFn, name }) | ||
|
||
try { | ||
await t.run() | ||
} catch (e) { | ||
console.log(c.error(e.message)) | ||
} | ||
}, | ||
tasks, | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
packages/cli/src/commands/destroy/layout/__tests__/layout.test.js
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,36 @@ | ||
global.__dirname = __dirname | ||
jest.mock('fs') | ||
jest.mock('src/lib', () => { | ||
return { | ||
...require.requireActual('src/lib'), | ||
generateTemplate: () => '', | ||
} | ||
}) | ||
|
||
import fs from 'fs' | ||
|
||
import 'src/lib/test' | ||
|
||
import { files } from '../../../generate/layout/layout' | ||
import { tasks } from '../layout' | ||
|
||
beforeEach(() => { | ||
fs.__setMockFiles(files({ name: 'Blog' })) | ||
}) | ||
|
||
afterEach(() => { | ||
fs.__setMockFiles({}) | ||
jest.spyOn(fs, 'unlinkSync').mockClear() | ||
}) | ||
|
||
test('destroys layout files', async () => { | ||
const unlinkSpy = jest.spyOn(fs, 'unlinkSync') | ||
const t = tasks({ componentName: 'layout', filesFn: files, name: 'Blog' }) | ||
t.setRenderer('silent') | ||
|
||
return t.run().then(() => { | ||
const generatedFiles = Object.keys(files({ name: 'Blog' })) | ||
expect(generatedFiles.length).toEqual(unlinkSpy.mock.calls.length) | ||
generatedFiles.forEach((f) => expect(unlinkSpy).toHaveBeenCalledWith(f)) | ||
}) | ||
}) |
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,13 @@ | ||
import { files as layoutFiles } from '../../generate/layout/layout' | ||
import { createYargsForComponentDestroy } from '../helpers' | ||
|
||
export const { | ||
command, | ||
desc, | ||
builder, | ||
handler, | ||
tasks, | ||
} = createYargsForComponentDestroy({ | ||
componentName: 'layout', | ||
filesFn: layoutFiles, | ||
}) |
80 changes: 80 additions & 0 deletions
80
packages/cli/src/commands/destroy/page/__tests__/page.test.js
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,80 @@ | ||
global.__dirname = __dirname | ||
jest.mock('fs') | ||
jest.mock('src/lib', () => { | ||
return { | ||
...require.requireActual('src/lib'), | ||
generateTemplate: () => '', | ||
} | ||
}) | ||
|
||
import fs from 'fs' | ||
|
||
import 'src/lib/test' | ||
import { getPaths } from 'src/lib' | ||
|
||
import { files } from '../../../generate/page/page' | ||
import { tasks } from '../page' | ||
|
||
beforeEach(() => { | ||
fs.__setMockFiles({ | ||
...files({ name: 'About' }), | ||
[getPaths().web.routes]: [ | ||
'<Routes>', | ||
' <Route path="/about" page={AboutPage} name="about" />', | ||
' <Route path="/" page={HomePage} name="home" />', | ||
' <Route notfound page={NotFoundPage} />', | ||
'</Routes>', | ||
].join('\n'), | ||
}) | ||
}) | ||
|
||
afterEach(() => { | ||
fs.__setMockFiles({}) | ||
jest.spyOn(fs, 'unlinkSync').mockClear() | ||
}) | ||
|
||
test('destroys page files', async () => { | ||
const unlinkSpy = jest.spyOn(fs, 'unlinkSync') | ||
const t = tasks({ name: 'About' }) | ||
t.setRenderer('silent') | ||
|
||
return t._tasks[0].run().then(() => { | ||
cannikin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const generatedFiles = Object.keys(files({ name: 'About' })) | ||
expect(generatedFiles.length).toEqual(unlinkSpy.mock.calls.length) | ||
generatedFiles.forEach((f) => expect(unlinkSpy).toHaveBeenCalledWith(f)) | ||
}) | ||
}) | ||
|
||
test('cleans up route from Routes.js', async () => { | ||
const t = tasks({ name: 'About' }) | ||
t.setRenderer('silent') | ||
|
||
return t._tasks[1].run().then(() => { | ||
const routes = fs.readFileSync(getPaths().web.routes) | ||
expect(routes).toEqual( | ||
[ | ||
'<Routes>', | ||
' <Route path="/" page={HomePage} name="home" />', | ||
' <Route notfound page={NotFoundPage} />', | ||
'</Routes>', | ||
].join('\n') | ||
) | ||
}) | ||
}) | ||
|
||
test('cleans up route with a custom path from Routes.js', async () => { | ||
const t = tasks({ name: 'About', path: '/about-us' }) | ||
t.setRenderer('silent') | ||
|
||
return t._tasks[1].run().then(() => { | ||
const routes = fs.readFileSync(getPaths().web.routes) | ||
expect(routes).toEqual( | ||
[ | ||
'<Routes>', | ||
' <Route path="/" page={HomePage} name="home" />', | ||
' <Route notfound page={NotFoundPage} />', | ||
'</Routes>', | ||
].join('\n') | ||
) | ||
}) | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I tried this instead:
But it breaks in a weird way: