refactor(): Add isPackageConfig and isMobileConfig to environment information

Rules can now read whether the file is a package config or mobile config file from the fileInfo.
This commit is contained in:
Dominik Ferber
2015-10-02 00:23:10 +02:00
parent b6ca598cc2
commit 740f3cfc07
2 changed files with 63 additions and 13 deletions

View File

@@ -12,28 +12,28 @@ function matchFirst (dirs, list) {
return false
}
function isCompatibilityMode (pathList) {
var clientIndex = pathList.indexOf(folderNames.CLIENT)
function isCompatibilityMode (pathInProjectList) {
var clientIndex = pathInProjectList.indexOf(folderNames.CLIENT)
// file is directly in client-folder, so it can't be in COMPATIBILITY
if (pathList.length - 2 === clientIndex) {
if (pathInProjectList.length - 2 === clientIndex) {
return false
}
return pathList[clientIndex + 1] === folderNames.COMPATIBILITY
return pathInProjectList[clientIndex + 1] === folderNames.COMPATIBILITY
}
function determineEnvironment (pathList) {
function determineEnvironment (pathInProjectList) {
if (pathList[0] === folderNames.PUBLIC) {
if (pathInProjectList[0] === folderNames.PUBLIC) {
return ENVIRONMENT.PUBLIC
}
if (pathList[0] === folderNames.PRIVATE) {
if (pathInProjectList[0] === folderNames.PRIVATE) {
return ENVIRONMENT.PRIVATE
}
if (pathList.length > 2 && pathList[0] === folderNames.PACKAGES) {
if (pathInProjectList.length > 2 && pathInProjectList[0] === folderNames.PACKAGES) {
return ENVIRONMENT.PACKAGE
}
@@ -45,7 +45,7 @@ function determineEnvironment (pathList) {
]
// remove filename
const dirList = pathList.slice(0, -1)
const dirList = pathInProjectList.slice(0, -1)
const matchedEnvironment = matchFirst(dirList, specialFolders)
switch (matchedEnvironment) {
@@ -76,16 +76,34 @@ function stripPathPrefix (parent, child) {
return normalizedChild.substr(normalizedParent.length + 1)
}
function isMobileConfig (pathInProject) {
return pathInProject === 'mobile-config.js'
}
function isCompatibilityFile (environment, pathInProjectList) {
return environment === ENVIRONMENT.CLIENT && isCompatibilityMode(pathInProjectList)
}
function isPackageConfig (pathInProjectList) {
if (pathInProjectList.length !== 3) {
return false
}
return pathInProjectList[0] === folderNames.PACKAGES && pathInProjectList[2] === 'package.js'
}
function getMeteorFileInfo (rootPath, filename) {
const pathInProject = stripPathPrefix(rootPath, filename)
const pathList = pathInProject.split(path.sep)
const environment = determineEnvironment(pathList)
const pathInProjectList = pathInProject.split(path.sep)
const environment = determineEnvironment(pathInProjectList)
return {
path: pathInProject,
env: environment,
isCompatibilityFile: environment === ENVIRONMENT.CLIENT && isCompatibilityMode(pathList),
isInMeteorProject: true
isCompatibilityFile: isCompatibilityFile(environment, pathInProjectList),
isInMeteorProject: true,
isMobileConfig: isMobileConfig(pathInProject),
isPackageConfig: isPackageConfig(pathInProjectList)
}
}

View File

@@ -154,5 +154,37 @@ describe('getMeteorMeta', function () {
assert.equal(result.isInMeteorProject, true)
})
describe('mobile-config.js', function () {
it('is detected', function () {
var filename = path.join(rootPath, 'mobile-config.js')
var result = getMeteorMeta(rootPaths, filename)
assert.equal(result.isMobileConfig, true)
})
it('is not detected', function () {
var filename = path.join(rootPath, 'sub', 'mobile-config.js')
var result = getMeteorMeta(rootPaths, filename)
assert.equal(result.isMobileConfig, false)
})
})
describe('package.js', function () {
it('is detected', function () {
var filename = path.join(rootPath, 'packages', 'my-module', 'package.js')
var result = getMeteorMeta(rootPaths, filename)
assert.equal(result.isPackageConfig, true)
})
it('is not detected', function () {
var filename = path.join(rootPath, 'packages', 'package.js')
var result = getMeteorMeta(rootPaths, filename)
assert.equal(result.isPackageConfig, false)
})
})
})