Skip to content

Commit

Permalink
fix: erase resources folder content before pulling resources (#554)
Browse files Browse the repository at this point in the history
  • Loading branch information
y-lakhdar authored Nov 25, 2021
1 parent 9adc0b0 commit 22b30a2
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 8 deletions.
30 changes: 26 additions & 4 deletions packages/cli-e2e/__tests__/orgResources.specs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {CLI_EXEC_PATH, getConfig, getPathToHomedirEnvFile} from '../utils/cli';
import {ProcessManager} from '../utils/processManager';
import {Terminal} from '../utils/terminal/terminal';
import {config} from 'dotenv';
import {ensureDirSync} from 'fs-extra';
import {ensureDirSync, rmSync} from 'fs-extra';
import PlatformClient, {FieldTypes} from '@coveord/platform-client';
import {getPlatformClient} from '../utils/platform';
import {readdirSync} from 'fs';
Expand Down Expand Up @@ -92,13 +92,16 @@ describe('org:resources', () => {
const pullFromOrg = async (
targetOrg: string,
procManager: ProcessManager,
destinationPath: string
destinationPath: string,
additionalFlags: string[] = []
) => {
const args: string[] = [
CLI_EXEC_PATH,
'org:resources:pull',
`-t=${targetOrg}`,
'-o',
'--no-git',
...additionalFlags,
];

const pullTerminal = createNewTerminal(
Expand Down Expand Up @@ -250,23 +253,42 @@ describe('org:resources', () => {

describe('org:resources:pull', () => {
const destinationPath = join('new-snapshot-project');
const getResourceFolderContent = (projectPath: string) =>
readdirSync(join(projectPath, 'resources'));

beforeAll(() => {
beforeEach(() => {
rmSync(destinationPath, {recursive: true, force: true});
ensureDirSync(destinationPath);
});

it(
"should pull the org's content",
async () => {
await pullFromOrg(testOrgId, processManager, destinationPath);

const snapshotFiles = readdirSync(snapshotProjectPath);
const destinationFiles = readdirSync(destinationPath);

expect(snapshotFiles).toEqual(destinationFiles);
},
defaultTimeout
);

it(
'directory should only contains pulled resources',
async () => {
await pullFromOrg(testOrgId, processManager, destinationPath, [
'-r=field',
]);
const originalResources = getResourceFolderContent(snapshotProjectPath);
const destinationResources = getResourceFolderContent(destinationPath);

expect(destinationResources.length).toBeGreaterThan(0);
expect(destinationResources.length).toBeLessThan(
originalResources.length
);
},
defaultTimeout
);
});

it('should not have any snapshot in the target org', async () => {
Expand Down
28 changes: 25 additions & 3 deletions packages/cli/src/commands/org/resources/pull.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ export default class Pull extends Command {
default: true,
allowNo: true,
}),
overwrite: flags.boolean({
char: 'o',
description: 'Overwrite resources directory if it exists.',
default: false,
}),
};

public static hidden = true;
Expand All @@ -70,10 +75,13 @@ export default class Pull extends Command {
HasNecessaryCoveoPrivileges(writeSnapshotPrivilege)
)
public async run() {
const project = new Project(this.projectPath);
await this.ensureProjectReset(project);

const snapshot = await this.getSnapshot();

cli.action.start('Updating project with Snapshot');
await this.refreshProject(snapshot);
await this.refreshProject(project, snapshot);

await snapshot.delete();
cli.action.stop('Project updated');
Expand Down Expand Up @@ -103,9 +111,8 @@ export default class Pull extends Command {
}
}

private async refreshProject(snapshot: Snapshot) {
private async refreshProject(project: Project, snapshot: Snapshot) {
const {flags} = this.parse(Pull);
const project = new Project(this.projectPath);
if (flags.git && !project.contains('.git')) {
await spawnProcess('git', ['init', `${this.projectPath}`], {
stdio: 'ignore',
Expand All @@ -115,6 +122,21 @@ export default class Pull extends Command {
await project.refresh(snapshotBlob);
}

private async ensureProjectReset(project: Project) {
const {flags} = this.parse(Pull);
if (!flags.overwrite && project.contains(Project.resourceFolderName)) {
const overwrite =
await cli.confirm(dedent`There is already a Coveo project with resources in it.
This command will overwrite the ${Project.resourceFolderName} folder content, do you want to proceed? (y/n)`);

if (!overwrite) {
this.exit();
}
}

project.reset();
}

private async getSnapshot() {
const {flags} = this.parse(Pull);
const target = await getTargetOrg(this.configuration, flags.target);
Expand Down
10 changes: 9 additions & 1 deletion packages/cli/src/lib/project/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
readdirSync,
unlinkSync,
writeFileSync,
rmSync,
} from 'fs';
import {extname, join} from 'path';
import {cli} from 'cli-ux';
Expand All @@ -14,7 +15,7 @@ import {DotFolder, DotFolderConfig} from './dotFolder';
import {readJsonSync, writeJsonSync, WriteOptions} from 'fs-extra';

export class Project {
private static readonly resourceFolderName = 'resources';
public static readonly resourceFolderName = 'resources';
public static readonly jsonFormat: WriteOptions = {spaces: '\t'};
public constructor(private _pathToProject: string) {
if (!this.isCoveoProject) {
Expand All @@ -31,6 +32,12 @@ export class Project {
this.deleteTemporaryZipFile();
}

public reset() {
if (this.isResourcesProject) {
rmSync(Project.resourceFolderName, {recursive: true, force: true});
}
}

private formatResourceFiles(dirPath = this.resourcePath) {
const files = readdirSync(dirPath, {withFileTypes: true});
files.forEach((file) => {
Expand Down Expand Up @@ -98,6 +105,7 @@ export class Project {
public get resourcePath() {
return join(this._pathToProject, Project.resourceFolderName);
}

public contains(fileName: string) {
return existsSync(join(this.pathToProject, fileName));
}
Expand Down

0 comments on commit 22b30a2

Please sign in to comment.