Skip to content

Commit

Permalink
Merge pull request #210 from sasjs/issue-209
Browse files Browse the repository at this point in the history
feat: support a user-level ~/.sasjslint
  • Loading branch information
allanbowe authored Apr 13, 2023
2 parents 22cc424 + 2cb73da commit 04cfa45
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
5 changes: 3 additions & 2 deletions src/format/formatProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import { formatFolder } from './formatFolder'
* @returns {Promise<FormatResult>} Resolves successfully when all SAS files in the current project have been formatted.
*/
export const formatProject = async (): Promise<FormatResult> => {
const projectRoot =
(await getProjectRoot()) || process.projectDir || process.currentDir
const projectRoot = (await getProjectRoot()) || process.currentDir
if (!projectRoot) {
throw new Error('SASjs Project Root was not found.')
}

console.info(`Formatting all .sas files under ${projectRoot}`)

return await formatFolder(projectRoot)
}
6 changes: 4 additions & 2 deletions src/lint/lintProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import { lintFolder } from './lintFolder'
* @returns {Promise<Map<string, Diagnostic[]>>} Resolves with a map with array of diagnostic objects, each containing a warning, line number and column number, and grouped by file path.
*/
export const lintProject = async () => {
const projectRoot =
(await getProjectRoot()) || process.projectDir || process.currentDir
const projectRoot = (await getProjectRoot()) || process.currentDir
if (!projectRoot) {
throw new Error('SASjs Project Root was not found.')
}

console.info(`Linting all .sas files under ${projectRoot}`)

return await lintFolder(projectRoot)
}
6 changes: 4 additions & 2 deletions src/utils/getLintConfig.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import path from 'path'
import os from 'os'
import { LintConfig } from '../types/LintConfig'
import { readFile } from '@sasjs/utils/file'
import { getProjectRoot } from './getProjectRoot'
Expand Down Expand Up @@ -31,14 +32,15 @@ export const DefaultLintConfiguration = {
}

/**
* Fetches the config from the .sasjslint file and creates a LintConfig object.
* Fetches the config from the .sasjslint file (at project root or home directory) and creates a LintConfig object.
* Returns the default configuration when a .sasjslint file is unavailable.
* @returns {Promise<LintConfig>} resolves with an object representing the current lint configuration.
*/
export async function getLintConfig(): Promise<LintConfig> {
const projectRoot = await getProjectRoot()
const lintFileLocation = projectRoot || os.homedir()
const configuration = await readFile(
path.join(projectRoot, '.sasjslint')
path.join(lintFileLocation, '.sasjslint')
).catch((_) => {
return JSON.stringify(DefaultLintConfiguration)
})
Expand Down
4 changes: 3 additions & 1 deletion src/utils/getProjectRoot.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import path from 'path'
import os from 'os'
import { fileExists } from '@sasjs/utils/file'

/**
Expand All @@ -11,10 +12,11 @@ export async function getProjectRoot(): Promise<string> {
let rootFound = false
let i = 1
let currentLocation = process.cwd()
const homeDir = os.homedir()

const maxLevels = currentLocation.split(path.sep).length

while (i <= maxLevels && !rootFound) {
while (i <= maxLevels && !rootFound && currentLocation !== homeDir) {
const isRoot = await fileExists(path.join(currentLocation, '.sasjslint'))

if (isRoot) {
Expand Down

0 comments on commit 04cfa45

Please sign in to comment.