Skip to content

Commit

Permalink
chore: adjust CLI to meet FSP Spec (#2491)
Browse files Browse the repository at this point in the history
## What's the purpose of this pull request?

To work with the Platform CLI, the FastStore CLI commands needs to
accept more arguments. I've added such arguments but they are not being
used, so this should not affect current behavior in any way.

## How to test it?

Install the version generated by the codesandbox and call all commands
:)

### Starters Deploy Preview

[Code](vtex-sites/starter.store#566) | [Store
(Vercel)](https://starter-rnshhkjja-faststore.vercel.app/) | [Store
(Homebrew)](https://sfj-164c14b--starter.preview.vtex.app/)

## References

[FastStore Platform
Spec](https://github.com/vtex/faststore-platform/blob/main/docs/specification.md)

---------

Co-authored-by: Larícia Mota <laricia.mota@vtex.com.br>
  • Loading branch information
gvc and lariciamota authored Oct 8, 2024
1 parent 7d80430 commit d88a361
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 13 deletions.
5 changes: 5 additions & 0 deletions packages/cli/src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import { generate } from '../utils/generate'

export default class Build extends Command {
static args = [
{
name: 'account',
description:
'The account for which the Discovery is running. Currently noop.',
},
{
name: 'path',
description:
Expand Down
21 changes: 16 additions & 5 deletions packages/cli/src/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const defaultIgnored = [

const devAbortController = new AbortController()

async function storeDev(rootDir: string, tmpDir: string, coreDir: string) {
async function storeDev(rootDir: string, tmpDir: string, coreDir: string, port: number) {
const envVars = dotenv.parse(readFileSync(path.join(rootDir, 'vtex.env')))

const packageManager = getPreferredPackageManager()
Expand All @@ -56,7 +56,7 @@ async function storeDev(rootDir: string, tmpDir: string, coreDir: string) {
console.log(`Attempted to copy from ${path.join(tmpDir, '@generated')} to ${path.join(coreDir, '@generated')}`)
}

const devProcess = spawn(`${packageManager} dev-only`, {
const devProcess = spawn(`${packageManager} dev-only --port ${port}`, {
shell: true,
cwd: tmpDir,
signal: devAbortController.signal,
Expand Down Expand Up @@ -84,15 +84,26 @@ function copyGenerated(from: string, to: string) {

export default class Dev extends Command {
static args = [
{
name: 'account',
description:
'The account for which the Discovery is running. Currently noop.',
},
{
name: 'path',
description: 'The path where the FastStore being run is. Defaults to cwd.',
}
description:
'The path where the FastStore being run is. Defaults to cwd.',
},
{
name: 'port',
description: 'The port where FastStore should run. Defaults to 3000.',
},
]

async run() {
const { args } = await this.parse(Dev)
const basePath = args.path ?? process.cwd()
const port = args.port ?? 3000

const { getRoot, tmpDir, coreDir } = withBasePath(basePath)

Expand All @@ -118,7 +129,7 @@ export default class Dev extends Command {

await generate({ setup: true, basePath })

storeDev(getRoot(), tmpDir, coreDir)
storeDev(getRoot(), tmpDir, coreDir, port)

return await new Promise((resolve, reject) => {
watcher
Expand Down
17 changes: 14 additions & 3 deletions packages/cli/src/commands/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,26 @@ import { getPreferredPackageManager } from '../utils/commands'

export default class Start extends Command {
static args = [
{
name: 'account',
description:
'The account for which the Discovery is running. Currently noop.',
},
{
name: 'path',
description: 'The path where the FastStore being run is. Defaults to cwd.',
}
description:
'The path where the FastStore being run is. Defaults to cwd.',
},
{
name: 'port',
description: 'The port where FastStore should run. Defaults to 3000.',
},
]

async run() {
const { args } = await this.parse(Start)
const basePath = args.path ?? process.cwd()
const port = args.port ?? 3000
const { tmpDir } = withBasePath(basePath)
const packageManager = getPreferredPackageManager()

Expand All @@ -24,7 +35,7 @@ export default class Start extends Command {
)
}

return spawn(`${packageManager} run serve`, {
return spawn(`${packageManager} run serve -p ${port}`, {
shell: true,
cwd: tmpDir,
stdio: 'inherit',
Expand Down
16 changes: 14 additions & 2 deletions packages/cli/src/utils/directory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ const pathsToMatch = (expected: string, desired: string) => {
return expectedResolved === desiredResolved
}

const originalProcessCwd = process.cwd

beforeEach(() => {
process.cwd = originalProcessCwd
})

describe('withBasePath as the current dir `.`', () => {
const basePath = '.'

Expand Down Expand Up @@ -79,8 +85,6 @@ describe('withBasePath as the current dir `.`', () => {
it('returns the path of the user discovery.config file', () => {
const { userStoreConfigFile: userStoreConfigFileWithBase } = withBasePath(basePath)

console.log('userStoreConfigFileWithBase', userStoreConfigFileWithBase);

expect(pathsToMatch(userStoreConfigFileWithBase, './discovery.config.js')).toBe(true)
})
})
Expand All @@ -99,13 +103,18 @@ describe('withBasePath as an arbitrary dir', () => {

describe('coreDir', () => {
it('is the faststoreDir + core', () => {
const mockedCwd = jest.fn(() => { return './src/__mocks__/store' })
process.cwd = mockedCwd
const { coreDir: coreDirWithBase } = withBasePath(basePath)

expect(pathsToMatch(coreDirWithBase, './src/__mocks__/store/node_modules/@faststore/core')).toBe(true)
})

describe('when is in a monorepo', () => {
it('can look at its parent until it reaches the monorepo directory', () => {
const mockedCwd = jest.fn(() => { return './src/__mocks__/monorepo' })
process.cwd = mockedCwd

const { coreDir: coreDirWithBase } = withBasePath(path.join(__dirname, '..', '__mocks__', 'monorepo', 'discovery'))

expect(pathsToMatch(coreDirWithBase, './src/__mocks__/monorepo/node_modules/@faststore/core')).toBe(true)
Expand Down Expand Up @@ -171,6 +180,9 @@ describe('withBasePath as an arbitrary dir', () => {

describe('coreCMSDir', () => {
it('returns the path of the CMS dir on @faststore/core package', () => {
const mockedCwd = jest.fn(() => { return './src/__mocks__/store' })
process.cwd = mockedCwd

const { coreCMSDir: coreCMSDirWithBase } = withBasePath(basePath)

expect(pathsToMatch(coreCMSDirWithBase, './src/__mocks__/store/node_modules/@faststore/core/cms/faststore')).toBe(true)
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/src/utils/directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ export const withBasePath = (basepath: string) => {
if (basepath.endsWith(tmpFolderName)) {
// if the current working directory is the build folder (tmp folder), return the starter root
// this makes sure the semantics of the starter root are consistent with the directories declared below
return path.join(basepath, '..')
return path.resolve(process.cwd(), path.join(basepath, '..'))
}

return basepath
return path.resolve(process.cwd(), basepath)
}

/*
Expand All @@ -31,7 +31,7 @@ export const withBasePath = (basepath: string) => {

let attemptedPath
do {
attemptedPath = path.join(basepath, ...parents, coreFromNodeModules)
attemptedPath = path.join(resolvedCwd, basepath, ...parents, coreFromNodeModules)

if (fs.existsSync(attemptedPath)) {
return attemptedPath
Expand Down

0 comments on commit d88a361

Please sign in to comment.