@@ -2,10 +2,11 @@ import { Yok } from "../../lib/common/yok";
22import { assert } from "chai" ;
33import { ProjectDataService } from "../../lib/services/project-data-service" ;
44import { LoggerStub , ProjectDataStub } from "../stubs" ;
5- import { NATIVESCRIPT_PROPS_INTERNAL_DELIMITER , PACKAGE_JSON_FILE_NAME , AssetConstants , ProjectTypes } from '../../lib/constants' ;
5+ import { NATIVESCRIPT_PROPS_INTERNAL_DELIMITER , PACKAGE_JSON_FILE_NAME , CONFIG_NS_FILE_NAME , AssetConstants , ProjectTypes } from '../../lib/constants' ;
66import { DevicePlatformsConstants } from "../../lib/common/mobile/device-platforms-constants" ;
77import { basename , join } from "path" ;
88import { FileSystem } from "../../lib/common/file-system" ;
9+ import { regExpEscape } from "../../lib/common/helpers" ;
910
1011const CLIENT_NAME_KEY_IN_PROJECT_FILE = "nativescript" ;
1112
@@ -41,7 +42,7 @@ const testData: any = [
4142 }
4243] ;
4344
44- const createTestInjector = ( readTextData ?: string ) : IInjector => {
45+ const createTestInjector = ( packageJsonContent ?: string , nsConfigContent ?: string ) : IInjector => {
4546 const testInjector = new Yok ( ) ;
4647 testInjector . register ( "projectData" , ProjectDataStub ) ;
4748 testInjector . register ( "staticConfig" , {
@@ -55,10 +56,14 @@ const createTestInjector = (readTextData?: string): IInjector => {
5556 } ,
5657
5758 readText : ( filename : string , encoding ?: IReadFileOptions | string ) : string => {
58- return readTextData ;
59+ if ( filename . indexOf ( "package.json" ) > - 1 ) {
60+ return packageJsonContent ;
61+ } else if ( filename . indexOf ( "nsconfig.json" ) > - 1 ) {
62+ return nsConfigContent ;
63+ }
5964 } ,
6065
61- exists : ( filePath : string ) : boolean => basename ( filePath ) === PACKAGE_JSON_FILE_NAME ,
66+ exists : ( filePath : string ) : boolean => ( basename ( filePath ) === PACKAGE_JSON_FILE_NAME || basename ( filePath ) === CONFIG_NS_FILE_NAME ) ,
6267
6368 readJson : ( filePath : string ) : any => null ,
6469
@@ -245,6 +250,66 @@ describe("projectDataService", () => {
245250 } ) ;
246251 } ) ;
247252
253+ describe ( "removeNSConfigProperty" , ( ) => {
254+
255+ const generateExpectedDataFromTestData = ( currentTestData : any ) => {
256+ const props = currentTestData . propertyName . split ( NATIVESCRIPT_PROPS_INTERNAL_DELIMITER ) ;
257+ props . splice ( props . length - 1 , 1 ) ;
258+
259+ const data : any = { } ;
260+ let currentData : any = data ;
261+
262+ _ . each ( props , ( prop ) => {
263+ currentData = currentData [ prop ] = { } ;
264+ } ) ;
265+
266+ return data ;
267+ } ;
268+
269+ _ . each ( testData , currentTestData => {
270+
271+ it ( currentTestData . description , ( ) => {
272+ const testInjector = createTestInjector ( null , generateFileContentFromTestData ( currentTestData , true ) ) ;
273+ const fs : IFileSystem = testInjector . resolve ( "fs" ) ;
274+
275+ let dataPassedToWriteJson : any = null ;
276+ fs . writeJson = ( filename : string , data : any , space ?: string , encoding ?: string ) : void => {
277+ dataPassedToWriteJson = data ;
278+ } ;
279+
280+ const projectDataService : IProjectDataService = testInjector . resolve ( "projectDataService" ) ;
281+ const propDelimiterRegExp = new RegExp ( regExpEscape ( NATIVESCRIPT_PROPS_INTERNAL_DELIMITER ) , "g" ) ;
282+ const propertySelector = currentTestData . propertyName . replace ( propDelimiterRegExp , "." ) ;
283+ projectDataService . removeNSConfigProperty ( "projectDir" , propertySelector ) ;
284+
285+ assert . deepEqual ( dataPassedToWriteJson , generateExpectedDataFromTestData ( currentTestData ) ) ;
286+ } ) ;
287+
288+ } ) ;
289+
290+ it ( "removes only the selected property" , ( ) => {
291+ const initialData : any = { } ;
292+ initialData [ CLIENT_NAME_KEY_IN_PROJECT_FILE ] = {
293+ "root" : {
294+ "id" : "1" ,
295+ "constantItem" : "myValue"
296+ }
297+ } ;
298+
299+ const testInjector = createTestInjector ( JSON . stringify ( initialData ) ) ;
300+ const fs : IFileSystem = testInjector . resolve ( "fs" ) ;
301+
302+ let dataPassedToWriteJson : any = null ;
303+ fs . writeJson = ( filename : string , data : any , space ?: string , encoding ?: string ) : void => {
304+ dataPassedToWriteJson = data ;
305+ } ;
306+
307+ const projectDataService : IProjectDataService = testInjector . resolve ( "projectDataService" ) ;
308+ projectDataService . removeNSProperty ( "projectDir" , getPropertyName ( [ "root" , "id" ] ) ) ;
309+ assert . deepEqual ( dataPassedToWriteJson , { nativescript : { root : { constantItem : "myValue" } } } ) ;
310+ } ) ;
311+ } ) ;
312+
248313 describe ( "removeDependency" , ( ) => {
249314 it ( "removes specified dependency from project file" , ( ) => {
250315 const currentTestData = {
0 commit comments