Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Lazy / stateless installation (#1675)
* Throw error when calling install if core version is managed by a workflow_settings.yaml * Make the compile method stateless * Make installation happen in a temporary directory, remove package-lock usage * Fix lint * Make installation happen in the temporary directory, add test for weird files * Fix lint * Fix tests * Fix tests
- Loading branch information
Showing
17 changed files
with
238 additions
and
112 deletions.
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
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
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
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 |
---|---|---|
@@ -1,55 +1,30 @@ | ||
import * as fs from "fs"; | ||
import { load as loadYaml, YAMLException } from "js-yaml"; | ||
import * as path from "path"; | ||
import { promisify } from "util"; | ||
|
||
import * as childProcess from "child_process"; | ||
import { dataform } from "df/protos/ts"; | ||
import { readDataformCoreVersionFromWorkflowSettings } from "df/cli/api/utils"; | ||
|
||
export async function install(projectPath: string, skipInstall?: boolean) { | ||
if (skipInstall) { | ||
return; | ||
} | ||
export const MISSING_CORE_VERSION_ERROR = | ||
"dataformCoreVersion must be specified either in workflow_settings.yaml or via a package.json"; | ||
|
||
export async function install(projectPath: string) { | ||
const resolvedProjectPath = path.resolve(projectPath); | ||
const workflowSettingsPath = path.join(resolvedProjectPath, "workflow_settings.yaml"); | ||
const packageJsonPath = path.join(resolvedProjectPath, "package.json"); | ||
const packageLockJsonPath = path.join(resolvedProjectPath, "package-lock.json"); | ||
|
||
let installCommand = "npm i --ignore-scripts"; | ||
|
||
// Core's readWorkflowSettings method cannot be used for this because Core assumes that | ||
// `require` can read YAML files directly. | ||
const dataformCoreVersion = readDataformCoreVersionIfPresent(workflowSettingsPath); | ||
|
||
const dataformCoreVersion = readDataformCoreVersionFromWorkflowSettings(resolvedProjectPath); | ||
if (dataformCoreVersion) { | ||
// If there are other packages already in the package.json, specifying a specific package to | ||
// install will trigger the other packages to be installed too. | ||
installCommand += ` @dataform/core@${dataformCoreVersion}`; | ||
} | ||
|
||
if (!dataformCoreVersion && !fs.existsSync(packageJsonPath)) { | ||
throw new Error( | ||
"dataformCoreVersion must be specified either in workflow_settings.yaml or via a package.json" | ||
"Package installation is only supported when specifying @dataform/core version in " + | ||
"'package.json'" | ||
); | ||
} | ||
|
||
await promisify(childProcess.exec)(installCommand, { cwd: resolvedProjectPath }); | ||
} | ||
|
||
function readDataformCoreVersionIfPresent(workflowSettingsPath: string): string { | ||
if (!fs.existsSync(workflowSettingsPath)) { | ||
return ""; | ||
if (!fs.existsSync(packageJsonPath)) { | ||
throw new Error(MISSING_CORE_VERSION_ERROR); | ||
} | ||
|
||
const workflowSettingsContent = fs.readFileSync(workflowSettingsPath, "utf-8"); | ||
let workflowSettingsAsJson = {}; | ||
try { | ||
workflowSettingsAsJson = loadYaml(workflowSettingsContent); | ||
} catch (e) { | ||
if (e instanceof YAMLException) { | ||
throw Error(`${path} is not a valid YAML file: ${e}`); | ||
} | ||
throw e; | ||
} | ||
return dataform.WorkflowSettings.create(workflowSettingsAsJson).dataformCoreVersion; | ||
await promisify(childProcess.exec)("npm i --ignore-scripts", { cwd: resolvedProjectPath }); | ||
} |
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 |
---|---|---|
@@ -1,3 +1,30 @@ | ||
import * as fs from "fs-extra"; | ||
import { load as loadYaml, YAMLException } from "js-yaml"; | ||
import * as path from "path"; | ||
|
||
import { dataform } from "df/protos/ts"; | ||
|
||
export function prettyJsonStringify(obj: object) { | ||
return JSON.stringify(obj, null, 4) + "\n"; | ||
} | ||
|
||
export function readDataformCoreVersionFromWorkflowSettings( | ||
resolvedProjectPath: string | ||
): string | undefined { | ||
const workflowSettingsPath = path.join(resolvedProjectPath, "workflow_settings.yaml"); | ||
if (!fs.existsSync(workflowSettingsPath)) { | ||
return; | ||
} | ||
|
||
const workflowSettingsContent = fs.readFileSync(workflowSettingsPath, "utf-8"); | ||
let workflowSettingsAsJson = {}; | ||
try { | ||
workflowSettingsAsJson = loadYaml(workflowSettingsContent); | ||
} catch (e) { | ||
if (e instanceof YAMLException) { | ||
throw new Error(`${path} is not a valid YAML file: ${e}`); | ||
} | ||
throw e; | ||
} | ||
return dataform.WorkflowSettings.create(workflowSettingsAsJson).dataformCoreVersion; | ||
} |
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
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
Oops, something went wrong.