Skip to content
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(core): introduce nx import #26847

Merged
merged 1 commit into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions e2e/nx/src/import.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import {
checkFilesExist,
cleanupProject,
getSelectedPackageManager,
newProject,
runCLI,
updateJson,
updateFile,
e2eCwd,
} from '@nx/e2e/utils';
import { mkdirSync, rmdirSync } from 'fs';
import { execSync } from 'node:child_process';
import { join } from 'path';

describe('Nx Import', () => {
let proj: string;
const tempImportE2ERoot = join(e2eCwd, 'nx-import');
beforeAll(() => {
proj = newProject({
packages: ['@nx/js'],
unsetProjectNameAndRootFormat: false,
});

if (getSelectedPackageManager() === 'pnpm') {
updateFile(
'pnpm-workspace.yaml',
`packages:
- 'projects/*'
`
);
} else {
updateJson('package.json', (json) => {
json.workspaces = ['projects/*'];
return json;
});
}

try {
rmdirSync(join(tempImportE2ERoot));
} catch {}
});
afterAll(() => cleanupProject());

it('should be able to import a vite app', () => {
mkdirSync(join(tempImportE2ERoot), { recursive: true });
const tempViteProjectName = 'created-vite-app';
execSync(
`npx create-vite@latest ${tempViteProjectName} --template react-ts`,
{
cwd: tempImportE2ERoot,
}
);
const tempViteProjectPath = join(tempImportE2ERoot, tempViteProjectName);
execSync(`git init`, {
cwd: tempViteProjectPath,
});
execSync(`git add .`, {
cwd: tempViteProjectPath,
});
execSync(`git commit -am "initial commit"`, {
cwd: tempViteProjectPath,
});
Comment on lines +60 to +62
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we create an extra commit or two? Just want to be safe :)

execSync(`git checkout -b main`, {
cwd: tempViteProjectPath,
});

const remote = tempViteProjectPath;
const ref = 'main';
const source = '.';
const directory = 'projects/vite-app';

runCLI(
`import ${remote} ${directory} --ref ${ref} --source ${source} --no-interactive`,
{
verbose: true,
}
);

checkFilesExist(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make some assertion about the git history of the project files? I trust that it is working, but just to keep it that way.

'projects/vite-app/.gitignore',
'projects/vite-app/package.json',
'projects/vite-app/index.html',
'projects/vite-app/vite.config.ts',
'projects/vite-app/src/main.tsx',
'projects/vite-app/src/App.tsx'
);
runCLI(`vite:build created-vite-app`);
});
});
48 changes: 48 additions & 0 deletions packages/nx/src/command-line/import/command-object.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { CommandModule } from 'yargs';
import { linkToNxDevAndExamples } from '../yargs-utils/documentation';
import { withVerbose } from '../yargs-utils/shared-options';
import { handleErrors } from '../../utils/params';

export const yargsImportCommand: CommandModule = {
command: 'import [sourceRemoteUrl] [destination]',
describe: false,
builder: (yargs) =>
linkToNxDevAndExamples(
withVerbose(
yargs
.positional('sourceRemoteUrl', {
type: 'string',
description: 'The remote URL of the source to import',
})
.positional('destination', {
type: 'string',
description:
'The directory in the current workspace to import into',
})
.option('source', {
type: 'string',
description:
'The directory in the source repository to import from',
})
.option('ref', {
type: 'string',
description: 'The branch from the source repository to import',
})
.option('interactive', {
type: 'boolean',
description: 'Interactive mode',
default: true,
})
),
'import'
),
handler: async (args) => {
const exitCode = await handleErrors(
(args.verbose as boolean) ?? process.env.NX_VERBOSE_LOGGING === 'true',
async () => {
return (await import('./import')).importHandler(args as any);
}
);
process.exit(exitCode);
},
};
Loading