-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpip-sync.ts
More file actions
59 lines (48 loc) · 2.28 KB
/
pip-sync.ts
File metadata and controls
59 lines (48 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { CreatePlan, DestroyPlan, RefreshContext, Resource, ResourceSettings, getPty } from 'codify-plugin-lib';
import { ResourceConfig } from 'codify-schemas';
import { codifySpawn } from '../../../utils/codify-spawn.js';
import schema from './pip-sync-schema.json'
import { RequirementFilesParameter } from './requirement-files-parameter.js';
export interface PipSyncConfig extends ResourceConfig {
requirementFiles: string[];
virtualEnv?: string;
cwd?: string;
}
export class PipSync extends Resource<PipSyncConfig> {
getSettings(): ResourceSettings<PipSyncConfig> {
return {
id: 'pip-sync',
schema,
parameterSettings: {
requirementFiles: { type: 'stateful', definition: new RequirementFilesParameter() },
virtualEnv: { type: 'directory', setting: true },
cwd: { type: 'directory', setting: true }
},
dependencies: ['pyenv', 'pip', 'venv-project', 'virtualenv-project', 'virtualenv'],
allowMultiple: {
identifyingParameters: ['virtualEnv'],
}
};
}
async refresh(parameters: Partial<PipSyncConfig>, context: RefreshContext<PipSyncConfig>): Promise<Partial<PipSyncConfig> | Partial<PipSyncConfig>[] | null> {
const pty = getPty()
const { status: pipStatus } = await pty.spawnSafe(PipSync.withVirtualEnv('which pip', parameters.virtualEnv), { cwd: parameters.cwd ?? undefined });
if (pipStatus === 'error') {
return null;
}
const { status: pipSyncStatus } = await pty.spawnSafe(PipSync.withVirtualEnv('which pip-sync', parameters.virtualEnv), { cwd: parameters.cwd ?? undefined })
return pipSyncStatus === 'error' ? null : parameters;
}
async create(plan: CreatePlan<PipSyncConfig>): Promise<void> {
await codifySpawn(PipSync.withVirtualEnv('pip install pip-tools', plan.desiredConfig.virtualEnv), { cwd: plan.desiredConfig.cwd ?? undefined })
}
async destroy(plan: DestroyPlan<PipSyncConfig>): Promise<void> {
await codifySpawn(PipSync.withVirtualEnv('pip uninstall -y pip-tools', plan.currentConfig.virtualEnv), { cwd: plan.currentConfig.cwd ?? undefined })
}
static withVirtualEnv(command: string, virtualEnv?: string, ): string {
if (!virtualEnv) {
return command;
}
return `( set -e; source ${virtualEnv}/bin/activate; ${command}; deactivate )`;
}
}